github.com/openshift/installer@v1.4.17/pkg/asset/agent/manifests/agent_test.go (about)

     1  package manifests
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	corev1 "k8s.io/api/core/v1"
     9  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  
    11  	hiveext "github.com/openshift/assisted-service/api/hiveextension/v1beta1"
    12  	aiv1beta1 "github.com/openshift/assisted-service/api/v1beta1"
    13  	"github.com/openshift/assisted-service/models"
    14  	hivev1 "github.com/openshift/hive/apis/hive/v1"
    15  	"github.com/openshift/installer/pkg/asset"
    16  )
    17  
    18  func TestAgentManifests_Generate(t *testing.T) {
    19  
    20  	fakeSecret := &corev1.Secret{
    21  		ObjectMeta: v1.ObjectMeta{Name: "fake-secret"},
    22  	}
    23  	fakeInfraEnv := &aiv1beta1.InfraEnv{
    24  		ObjectMeta: v1.ObjectMeta{Name: "fake-infraEnv"},
    25  	}
    26  	fakeStaticNetworkConfig := []*models.HostStaticNetworkConfig{{NetworkYaml: "some-yaml"}}
    27  	fakeNMStatConfig := []*aiv1beta1.NMStateConfig{{ObjectMeta: v1.ObjectMeta{Name: "fake-nmState"}}}
    28  	fakeAgentClusterInstall := &hiveext.AgentClusterInstall{ObjectMeta: v1.ObjectMeta{Name: "fake-agentClusterInstall"}}
    29  	fakeClusterDeployment := &hivev1.ClusterDeployment{ObjectMeta: v1.ObjectMeta{Name: "fake-clusterDeployment"}}
    30  	fakeClusterImageSet := &hivev1.ClusterImageSet{ObjectMeta: v1.ObjectMeta{Name: "fake-clusterImageSet"}}
    31  
    32  	tests := []struct {
    33  		Name                        string
    34  		Assets                      []asset.WritableAsset
    35  		ExpectedPullSecret          *corev1.Secret
    36  		ExpectedInfraEnv            *aiv1beta1.InfraEnv
    37  		ExpectedStaticNetworkConfig []*models.HostStaticNetworkConfig
    38  		ExpectedNMStateConfig       []*aiv1beta1.NMStateConfig
    39  		ExpectedAgentClusterInstall *hiveext.AgentClusterInstall
    40  		ExpectedClusterDeployment   *hivev1.ClusterDeployment
    41  		ExpectedClusterImageSet     *hivev1.ClusterImageSet
    42  		ExpectedError               string
    43  	}{
    44  		{
    45  			Name: "default",
    46  			Assets: []asset.WritableAsset{
    47  				&AgentPullSecret{Config: fakeSecret},
    48  				&InfraEnv{Config: fakeInfraEnv},
    49  				&NMStateConfig{
    50  					StaticNetworkConfig: fakeStaticNetworkConfig,
    51  					Config:              fakeNMStatConfig,
    52  				},
    53  				&AgentClusterInstall{Config: fakeAgentClusterInstall},
    54  				&ClusterDeployment{Config: fakeClusterDeployment},
    55  				&ClusterImageSet{Config: fakeClusterImageSet},
    56  			},
    57  			ExpectedPullSecret:          fakeSecret,
    58  			ExpectedInfraEnv:            fakeInfraEnv,
    59  			ExpectedStaticNetworkConfig: fakeStaticNetworkConfig,
    60  			ExpectedNMStateConfig:       fakeNMStatConfig,
    61  			ExpectedAgentClusterInstall: fakeAgentClusterInstall,
    62  			ExpectedClusterDeployment:   fakeClusterDeployment,
    63  			ExpectedClusterImageSet:     fakeClusterImageSet,
    64  		},
    65  		{
    66  			Name: "invalid-NMStateLabelSelector",
    67  			Assets: []asset.WritableAsset{
    68  				&AgentPullSecret{},
    69  				&InfraEnv{Config: &aiv1beta1.InfraEnv{
    70  					Spec: aiv1beta1.InfraEnvSpec{
    71  						NMStateConfigLabelSelector: v1.LabelSelector{
    72  							MatchLabels: map[string]string{
    73  								"missing-label": "missing-label",
    74  							},
    75  						},
    76  					},
    77  				}},
    78  				&NMStateConfig{
    79  					StaticNetworkConfig: fakeStaticNetworkConfig,
    80  					Config:              fakeNMStatConfig,
    81  				},
    82  				&AgentClusterInstall{},
    83  				&ClusterDeployment{},
    84  				&ClusterImageSet{},
    85  			},
    86  			ExpectedError: "invalid agent configuration: Spec.NMStateConfigLabelSelector.MatchLabels: Required value: infraEnv and fake-nmState.NMStateConfig labels do not match. Expected: map[missing-label:missing-label] Found: map[]",
    87  		},
    88  	}
    89  	for _, tt := range tests {
    90  		t.Run(tt.Name, func(t *testing.T) {
    91  			m := &AgentManifests{}
    92  
    93  			fakeParent := asset.Parents{}
    94  			for _, a := range tt.Assets {
    95  				fakeParent.Add(a)
    96  			}
    97  
    98  			err := m.Generate(context.Background(), fakeParent)
    99  			if tt.ExpectedError != "" {
   100  				assert.Equal(t, tt.ExpectedError, err.Error())
   101  			} else {
   102  				assert.NoError(t, err)
   103  			}
   104  			if tt.ExpectedPullSecret != nil {
   105  				assert.Equal(t, tt.ExpectedPullSecret, m.PullSecret)
   106  			}
   107  			if tt.ExpectedInfraEnv != nil {
   108  				assert.Equal(t, tt.ExpectedInfraEnv, m.InfraEnv)
   109  			}
   110  			if tt.ExpectedStaticNetworkConfig != nil {
   111  				assert.Equal(t, tt.ExpectedStaticNetworkConfig, m.StaticNetworkConfigs)
   112  			}
   113  			if tt.ExpectedNMStateConfig != nil {
   114  				assert.Equal(t, tt.ExpectedNMStateConfig, m.NMStateConfigs)
   115  			}
   116  			if tt.ExpectedClusterDeployment != nil {
   117  				assert.Equal(t, tt.ExpectedClusterDeployment, m.ClusterDeployment)
   118  			}
   119  			if tt.ExpectedClusterImageSet != nil {
   120  				assert.Equal(t, tt.ExpectedClusterImageSet, m.ClusterImageSet)
   121  			}
   122  		})
   123  	}
   124  }