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

     1  package image
     2  
     3  import (
     4  	"context"
     5  	"encoding/base64"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	v1 "k8s.io/api/core/v1"
    10  
    11  	aiv1beta1 "github.com/openshift/assisted-service/api/v1beta1"
    12  	"github.com/openshift/assisted-service/models"
    13  	hivev1 "github.com/openshift/hive/apis/hive/v1"
    14  	"github.com/openshift/installer/pkg/asset"
    15  	"github.com/openshift/installer/pkg/asset/agent/common"
    16  	"github.com/openshift/installer/pkg/asset/agent/manifests"
    17  	"github.com/openshift/installer/pkg/asset/agent/mirror"
    18  )
    19  
    20  func TestUnconfiguredIgnition_Generate(t *testing.T) {
    21  	skipTestIfnmstatectlIsMissing(t)
    22  
    23  	nmStateConfig := getTestNMStateConfig()
    24  
    25  	cases := []struct {
    26  		name              string
    27  		overrideDeps      []asset.Asset
    28  		expectedError     string
    29  		expectedFiles     []string
    30  		serviceEnabledMap map[string]bool
    31  	}{
    32  		{
    33  			name:          "default-configs-and-no-nmstateconfigs",
    34  			expectedFiles: generatedFilesUnconfiguredIgnition("/usr/local/bin/pre-network-manager-config.sh"),
    35  			serviceEnabledMap: map[string]bool{
    36  				"pre-network-manager-config.service": false,
    37  				"agent-check-config-image.service":   true},
    38  		},
    39  		{
    40  			name: "with-mirror-configs",
    41  			overrideDeps: []asset.Asset{
    42  				&mirror.RegistriesConf{
    43  					File: &asset.File{
    44  						Filename: mirror.RegistriesConfFilename,
    45  						Data:     []byte(""),
    46  					},
    47  					MirrorConfig: []mirror.RegistriesConfig{
    48  						{
    49  							Location: "some.registry.org/release",
    50  							Mirror:   "some.mirror.org",
    51  						},
    52  					},
    53  				},
    54  				&mirror.CaBundle{
    55  					File: &asset.File{
    56  						Filename: "my.crt",
    57  						Data:     []byte("my-certificate"),
    58  					},
    59  				},
    60  			},
    61  			expectedFiles: generatedFilesUnconfiguredIgnition(registriesConfPath,
    62  				registryCABundlePath, "/usr/local/bin/pre-network-manager-config.sh"),
    63  			serviceEnabledMap: map[string]bool{
    64  				"pre-network-manager-config.service": false,
    65  				"agent-check-config-image.service":   true},
    66  		},
    67  		{
    68  			name: "with-nmstateconfigs",
    69  			overrideDeps: []asset.Asset{
    70  				&nmStateConfig,
    71  			},
    72  			expectedFiles: generatedFilesUnconfiguredIgnition("/etc/assisted/network/host0/eth0.nmconnection",
    73  				"/etc/assisted/network/host0/mac_interface.ini", "/usr/local/bin/pre-network-manager-config.sh"),
    74  			serviceEnabledMap: map[string]bool{
    75  				"pre-network-manager-config.service": true,
    76  				"agent-check-config-image.service":   true},
    77  		},
    78  	}
    79  	for _, tc := range cases {
    80  		t.Run(tc.name, func(t *testing.T) {
    81  			deps := buildUnconfiguredIgnitionAssetDefaultDependencies(t)
    82  
    83  			overrideDeps(deps, tc.overrideDeps)
    84  
    85  			parents := asset.Parents{}
    86  			parents.Add(deps...)
    87  
    88  			unconfiguredIgnitionAsset := &UnconfiguredIgnition{}
    89  			err := unconfiguredIgnitionAsset.Generate(context.Background(), parents)
    90  
    91  			if tc.expectedError != "" {
    92  				assert.Equal(t, tc.expectedError, err.Error())
    93  			} else {
    94  				assert.NoError(t, err)
    95  
    96  				assertExpectedFiles(t, unconfiguredIgnitionAsset.Config, tc.expectedFiles, nil)
    97  
    98  				assertServiceEnabled(t, unconfiguredIgnitionAsset.Config, tc.serviceEnabledMap)
    99  			}
   100  		})
   101  	}
   102  }
   103  
   104  // This test util create the minimum valid set of dependencies for the
   105  // UnconfiguredIgnition asset.
   106  func buildUnconfiguredIgnitionAssetDefaultDependencies(t *testing.T) []asset.Asset {
   107  	t.Helper()
   108  
   109  	infraEnv := getTestInfraEnv()
   110  	agentPullSecret := getTestAgentPullSecret(t)
   111  	clusterImageSet := getTestClusterImageSet()
   112  
   113  	return []asset.Asset{
   114  		&infraEnv,
   115  		&agentPullSecret,
   116  		&clusterImageSet,
   117  		&manifests.NMStateConfig{},
   118  		&mirror.RegistriesConf{},
   119  		&mirror.CaBundle{},
   120  		&common.InfraEnvID{},
   121  	}
   122  }
   123  
   124  func getTestInfraEnv() manifests.InfraEnv {
   125  	return manifests.InfraEnv{
   126  		Config: &aiv1beta1.InfraEnv{
   127  			Spec: aiv1beta1.InfraEnvSpec{
   128  				SSHAuthorizedKey: "my-ssh-key",
   129  			},
   130  		},
   131  		File: &asset.File{
   132  			Filename: "infraenv.yaml",
   133  			Data:     []byte("infraenv"),
   134  		},
   135  	}
   136  }
   137  
   138  func getTestAgentPullSecret(t *testing.T) manifests.AgentPullSecret {
   139  	t.Helper()
   140  	secretDataBytes, err := base64.StdEncoding.DecodeString("c3VwZXItc2VjcmV0Cg==")
   141  	assert.NoError(t, err)
   142  	return manifests.AgentPullSecret{
   143  		Config: &v1.Secret{
   144  			Data: map[string][]byte{
   145  				".dockerconfigjson": secretDataBytes,
   146  			},
   147  		},
   148  		File: &asset.File{
   149  			Filename: "pull-secret.yaml",
   150  			Data:     []byte("pull-secret"),
   151  		},
   152  	}
   153  }
   154  
   155  func getTestClusterImageSet() manifests.ClusterImageSet {
   156  	return manifests.ClusterImageSet{
   157  		Config: &hivev1.ClusterImageSet{
   158  			Spec: hivev1.ClusterImageSetSpec{
   159  				ReleaseImage: "registry.ci.openshift.org/origin/release:4.11",
   160  			},
   161  		},
   162  		File: &asset.File{
   163  			Filename: "cluster-image-set.yaml",
   164  			Data:     []byte("cluster-image-set"),
   165  		},
   166  	}
   167  }
   168  
   169  func getTestNMStateConfig() manifests.NMStateConfig {
   170  	return manifests.NMStateConfig{
   171  		Config: []*aiv1beta1.NMStateConfig{
   172  			{
   173  				Spec: aiv1beta1.NMStateConfigSpec{
   174  					Interfaces: []*aiv1beta1.Interface{
   175  						{
   176  							Name:       "eth0",
   177  							MacAddress: "00:01:02:03:04:05",
   178  						},
   179  					},
   180  				},
   181  			},
   182  		},
   183  		StaticNetworkConfig: []*models.HostStaticNetworkConfig{
   184  			{
   185  				MacInterfaceMap: models.MacInterfaceMap{
   186  					{LogicalNicName: "eth0", MacAddress: "00:01:02:03:04:05"},
   187  				},
   188  				NetworkYaml: "interfaces:\n- ipv4:\n    address:\n    - ip: 192.168.122.21\n      prefix-length: 24\n    enabled: true\n  mac-address: 00:01:02:03:04:05\n  name: eth0\n  state: up\n  type: ethernet\n",
   189  			},
   190  		},
   191  		File: &asset.File{
   192  			Filename: "nmstateconfig.yaml",
   193  			Data:     []byte("nmstateconfig"),
   194  		},
   195  	}
   196  }
   197  
   198  func generatedFilesUnconfiguredIgnition(otherFiles ...string) []string {
   199  	unconfiguredIgnitionFiles := []string{
   200  		"/etc/assisted/manifests/pull-secret.yaml",
   201  		"/etc/assisted/manifests/cluster-image-set.yaml",
   202  		"/etc/assisted/manifests/infraenv.yaml",
   203  	}
   204  	unconfiguredIgnitionFiles = append(unconfiguredIgnitionFiles, otherFiles...)
   205  	return append(unconfiguredIgnitionFiles, commonFiles()...)
   206  }