@@ -18,6 +18,7 @@ package state
1818
1919import (
2020 "bytes"
21+ "context"
2122 "os"
2223 "path/filepath"
2324 "strings"
@@ -34,8 +35,10 @@ import (
3435 apitypes "k8s.io/apimachinery/pkg/types"
3536 "k8s.io/client-go/kubernetes/scheme"
3637 "k8s.io/utils/ptr"
38+ "sigs.k8s.io/controller-runtime/pkg/client/fake"
3739
3840 nvidiav1alpha1 "github.com/NVIDIA/gpu-operator/api/nvidia/v1alpha1"
41+ "github.com/NVIDIA/gpu-operator/internal/consts"
3942 "github.com/NVIDIA/gpu-operator/internal/render"
4043)
4144
@@ -44,6 +47,27 @@ const (
4447 manifestResultDir = "./testdata/golden"
4548)
4649
50+ type testClusterInfo struct {
51+ runtime string
52+ openshiftVersion string
53+ }
54+
55+ func (i testClusterInfo ) GetContainerRuntime () (string , error ) {
56+ return i .runtime , nil
57+ }
58+
59+ func (i testClusterInfo ) GetOpenshiftVersion () (string , error ) {
60+ return i .openshiftVersion , nil
61+ }
62+
63+ func (i testClusterInfo ) GetOpenshiftDriverToolkitImages () map [string ]string {
64+ return nil
65+ }
66+
67+ func (i testClusterInfo ) GetOpenshiftProxySpec () (* configv1.ProxySpec , error ) {
68+ return nil , nil
69+ }
70+
4771func getYAMLString (objs []* unstructured.Unstructured ) (string , error ) {
4872 s := json .NewSerializerWithOptions (json .DefaultMetaFactory , scheme .Scheme ,
4973 scheme .Scheme , json.SerializerOptions {Yaml : true , Pretty : false , Strict : false })
@@ -60,6 +84,35 @@ func getYAMLString(objs []*unstructured.Unstructured) (string, error) {
6084 return sb .String (), nil
6185}
6286
87+ func hasSubscriptionVolumeMount (volumeMounts []corev1.VolumeMount ) bool {
88+ for _ , volumeMount := range volumeMounts {
89+ if strings .HasPrefix (volumeMount .Name , "subscription-config-" ) {
90+ return true
91+ }
92+ }
93+ return false
94+ }
95+
96+ func assertSubscriptionHostPathVolumes (t * testing.T , volumes []corev1.Volume , expected map [string ]corev1.HostPathType ) {
97+ t .Helper ()
98+
99+ if expected == nil {
100+ expected = map [string ]corev1.HostPathType {}
101+ }
102+
103+ actual := map [string ]corev1.HostPathType {}
104+ for _ , volume := range volumes {
105+ if ! strings .HasPrefix (volume .Name , "subscription-config-" ) {
106+ continue
107+ }
108+ require .NotNil (t , volume .HostPath )
109+ require .NotNil (t , volume .HostPath .Type )
110+ actual [volume .HostPath .Path ] = * volume .HostPath .Type
111+ }
112+
113+ assert .Equal (t , expected , actual )
114+ }
115+
63116func TestDriverRenderMinimal (t * testing.T ) {
64117 // Construct a sample driver state manager
65118 const (
@@ -440,6 +493,69 @@ func TestDriverAdditionalConfigs(t *testing.T) {
440493 require .Equal (t , string (o ), actual )
441494}
442495
496+ func TestDriverAdditionalConfigsSubscriptionMounts (t * testing.T ) {
497+ repoConfigMap := & corev1.ConfigMap {
498+ ObjectMeta : metav1.ObjectMeta {
499+ Name : "test-repo-config" ,
500+ Namespace : "test-ns" ,
501+ },
502+ Data : map [string ]string {
503+ "redhat.repo" : "[test-repo]" ,
504+ },
505+ }
506+
507+ testCases := []struct {
508+ description string
509+ osRelease string
510+ repoConfigEnabled bool
511+ expectSubscriptionMounts bool
512+ expectedSubscriptionHostMap map [string ]corev1.HostPathType
513+ }{
514+ {
515+ description : "rhel with repo config skips host subscription mounts" ,
516+ osRelease : "rhel" ,
517+ repoConfigEnabled : true ,
518+ expectSubscriptionMounts : false ,
519+ },
520+ {
521+ description : "rhel without repo config mounts host subscription paths" ,
522+ osRelease : "rhel" ,
523+ expectSubscriptionMounts : true ,
524+ expectedSubscriptionHostMap : map [string ]corev1.HostPathType {
525+ "/etc/pki/entitlement" : corev1 .HostPathDirectory ,
526+ "/etc/yum.repos.d/redhat.repo" : corev1 .HostPathFile ,
527+ "/etc/rhsm" : corev1 .HostPathDirectory ,
528+ },
529+ },
530+ }
531+
532+ for _ , tc := range testCases {
533+ t .Run (tc .description , func (t * testing.T ) {
534+ stateDriver := & stateDriver {
535+ stateSkel : stateSkel {
536+ client : fake .NewClientBuilder ().WithScheme (scheme .Scheme ).WithObjects (repoConfigMap ).Build (),
537+ namespace : "test-ns" ,
538+ },
539+ }
540+ driver := & nvidiav1alpha1.NVIDIADriver {}
541+ if tc .repoConfigEnabled {
542+ driver .Spec .RepoConfig = & nvidiav1alpha1.DriverRepoConfigSpec {Name : "test-repo-config" }
543+ }
544+
545+ configs , err := stateDriver .getDriverAdditionalConfigs (
546+ context .Background (),
547+ driver ,
548+ testClusterInfo {runtime : consts .Containerd },
549+ nodePool {osRelease : tc .osRelease , osVersion : tc .osRelease },
550+ )
551+ require .NoError (t , err )
552+
553+ assertSubscriptionHostPathVolumes (t , configs .Volumes , tc .expectedSubscriptionHostMap )
554+ assert .Equal (t , tc .expectSubscriptionMounts , hasSubscriptionVolumeMount (configs .VolumeMounts ))
555+ })
556+ }
557+ }
558+
443559func TestDriverOpenshiftDriverToolkit (t * testing.T ) {
444560 const (
445561 testName = "driver-openshift-drivertoolkit"
0 commit comments