github.com/equinix-metal/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/tests/e2e/cloudinit_test.go (about)

     1  /*
     2  Copyright 2017 Mirantis
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package e2e
    18  
    19  import (
    20  	"time"
    21  
    22  	. "github.com/onsi/gomega"
    23  	"k8s.io/api/core/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  
    26  	"github.com/Mirantis/virtlet/tests/e2e/framework"
    27  	. "github.com/Mirantis/virtlet/tests/e2e/ginkgo-ext"
    28  )
    29  
    30  var _ = Describe("Cloud-init related tests", func() {
    31  	Context("VM with SSH key from implicit key of ConfigMap", func() {
    32  		var vm *framework.VMInterface
    33  
    34  		BeforeAll(func() {
    35  			cm := &v1.ConfigMap{
    36  				ObjectMeta: metav1.ObjectMeta{
    37  					Name: "cm-ssh-key-impl",
    38  				},
    39  				Data: map[string]string{
    40  					"authorized_keys": SshPublicKey,
    41  				},
    42  			}
    43  			_, err := controller.ConfigMaps().Create(cm)
    44  			Expect(err).NotTo(HaveOccurred())
    45  
    46  			vm = controller.VM("ssh-from-cm-impl")
    47  			Expect(vm.CreateAndWait(VMOptions{
    48  				SSHKeySource: "configmap/cm-ssh-key-impl",
    49  			}.ApplyDefaults(), time.Minute*5, nil)).To(Succeed())
    50  		})
    51  
    52  		AfterAll(func() {
    53  			deleteVM(vm)
    54  			controller.ConfigMaps().Delete("cm-ssh-key-impl", nil)
    55  		})
    56  
    57  		It("Should have SSH accessible [Conformance]", func() {
    58  			waitSSH(vm)
    59  		})
    60  	})
    61  
    62  	Context("VM with SSH key from explicit key of ConfigMap", func() {
    63  		var vm *framework.VMInterface
    64  
    65  		BeforeAll(func() {
    66  			cm := &v1.ConfigMap{
    67  				ObjectMeta: metav1.ObjectMeta{
    68  					Name: "cm-ssh-key-expl",
    69  				},
    70  				Data: map[string]string{
    71  					"myKey": SshPublicKey,
    72  				},
    73  			}
    74  			_, err := controller.ConfigMaps().Create(cm)
    75  			Expect(err).NotTo(HaveOccurred())
    76  
    77  			vm = controller.VM("ssh-from-cm-expl")
    78  			Expect(vm.CreateAndWait(VMOptions{
    79  				SSHKeySource: "configmap/cm-ssh-key-expl/myKey",
    80  			}.ApplyDefaults(), time.Minute*5, nil)).To(Succeed())
    81  		})
    82  
    83  		AfterAll(func() {
    84  			deleteVM(vm)
    85  			controller.ConfigMaps().Delete("cm-ssh-key-expl", nil)
    86  		})
    87  
    88  		It("Should have SSH accessible [Conformance]", func() {
    89  			waitSSH(vm)
    90  		})
    91  	})
    92  
    93  	Context("VM with SSH key from implicit key of Secret", func() {
    94  		var vm *framework.VMInterface
    95  
    96  		BeforeAll(func() {
    97  			secret := &v1.Secret{
    98  				ObjectMeta: metav1.ObjectMeta{
    99  					Name: "secret-ssh-key-impl",
   100  				},
   101  				StringData: map[string]string{
   102  					"authorized_keys": SshPublicKey,
   103  				},
   104  			}
   105  			_, err := controller.Secrets().Create(secret)
   106  			Expect(err).NotTo(HaveOccurred())
   107  
   108  			vm = controller.VM("ssh-from-secret-impl")
   109  			Expect(vm.CreateAndWait(VMOptions{
   110  				SSHKeySource: "secret/secret-ssh-key-impl",
   111  			}.ApplyDefaults(), time.Minute*5, nil)).To(Succeed())
   112  		})
   113  
   114  		AfterAll(func() {
   115  			deleteVM(vm)
   116  			controller.Secrets().Delete("secret-ssh-key-impl", nil)
   117  		})
   118  
   119  		It("Should have SSH accessible [Conformance]", func() {
   120  			waitSSH(vm)
   121  		})
   122  	})
   123  
   124  	Context("VM with SSH key from explicit key of Secret", func() {
   125  		var vm *framework.VMInterface
   126  
   127  		BeforeAll(func() {
   128  			secret := &v1.Secret{
   129  				ObjectMeta: metav1.ObjectMeta{
   130  					Name: "secret-ssh-key-expl",
   131  				},
   132  				StringData: map[string]string{
   133  					"myKey": SshPublicKey,
   134  				},
   135  			}
   136  			_, err := controller.Secrets().Create(secret)
   137  			Expect(err).NotTo(HaveOccurred())
   138  
   139  			vm = controller.VM("ssh-from-secret-expl")
   140  			Expect(vm.CreateAndWait(VMOptions{
   141  				SSHKeySource: "secret/secret-ssh-key-expl/myKey",
   142  			}.ApplyDefaults(), time.Minute*5, nil)).To(Succeed())
   143  		})
   144  
   145  		AfterAll(func() {
   146  			deleteVM(vm)
   147  			controller.Secrets().Delete("secret-ssh-key-expl", nil)
   148  		})
   149  
   150  		It("Should have SSH accessible [Conformance]", func() {
   151  			waitSSH(vm)
   152  		})
   153  	})
   154  
   155  	Context("User-data from ConfigMap", func() {
   156  		var vm *framework.VMInterface
   157  
   158  		const fileConf = `[{"content": "Hello world!", "path": "/tmp/test-file"}]`
   159  
   160  		BeforeAll(func() {
   161  			requireCloudInit()
   162  			cm := &v1.ConfigMap{
   163  				ObjectMeta: metav1.ObjectMeta{
   164  					Name: "cm-userdata",
   165  				},
   166  				Data: map[string]string{
   167  					"write_files": fileConf,
   168  				},
   169  			}
   170  			_, err := controller.ConfigMaps().Create(cm)
   171  			Expect(err).NotTo(HaveOccurred())
   172  
   173  			vm = controller.VM("userdata-cm")
   174  			Expect(vm.CreateAndWait(VMOptions{
   175  				UserDataSource: "configmap/cm-userdata",
   176  			}.ApplyDefaults(), time.Minute*5, nil)).To(Succeed())
   177  		})
   178  
   179  		AfterAll(func() {
   180  			deleteVM(vm)
   181  			controller.ConfigMaps().Delete("cm-userdata", nil)
   182  		})
   183  
   184  		It("Must be processed [Conformance]", func() {
   185  			ssh := waitSSH(vm)
   186  			Expect(framework.RunSimple(ssh, "cat", "/tmp/test-file")).To(Equal("Hello world!"))
   187  		})
   188  	})
   189  
   190  	Context("User-data from Secret", func() {
   191  		var vm *framework.VMInterface
   192  
   193  		const fileConf = `[{"content": "Hello world!", "path": "/tmp/test-file"}]`
   194  
   195  		BeforeAll(func() {
   196  			requireCloudInit()
   197  			secret := &v1.Secret{
   198  				ObjectMeta: metav1.ObjectMeta{
   199  					Name: "secret-userdata",
   200  				},
   201  				StringData: map[string]string{
   202  					"write_files": fileConf,
   203  				},
   204  			}
   205  			_, err := controller.Secrets().Create(secret)
   206  			Expect(err).NotTo(HaveOccurred())
   207  
   208  			vm = controller.VM("userdata-secret")
   209  			Expect(vm.CreateAndWait(VMOptions{
   210  				UserDataSource: "secret/secret-userdata",
   211  			}.ApplyDefaults(), time.Minute*5, nil)).To(Succeed())
   212  		})
   213  
   214  		AfterAll(func() {
   215  			deleteVM(vm)
   216  			controller.Secrets().Delete("secret-userdata", nil)
   217  		})
   218  
   219  		It("Must be processed [Conformance]", func() {
   220  			ssh := waitSSH(vm)
   221  			Expect(framework.RunSimple(ssh, "cat", "/tmp/test-file")).To(Equal("Hello world!"))
   222  		})
   223  	})
   224  
   225  	Context("User-data merged from two sources", func() {
   226  		var vm *framework.VMInterface
   227  
   228  		const fileConf = `[{"content": "Hello ", "path": "/tmp/test-file1"}]`
   229  		const userData = `{"write_files": [{"content": "world!", "path": "/tmp/test-file2"}]}`
   230  
   231  		BeforeAll(func() {
   232  			requireCloudInit()
   233  			cm := &v1.ConfigMap{
   234  				ObjectMeta: metav1.ObjectMeta{
   235  					Name: "cm-userdata",
   236  				},
   237  				Data: map[string]string{
   238  					"write_files": fileConf,
   239  				},
   240  			}
   241  			_, err := controller.ConfigMaps().Create(cm)
   242  			Expect(err).NotTo(HaveOccurred())
   243  
   244  			vm = controller.VM("userdata-cm-merge")
   245  			Expect(vm.CreateAndWait(VMOptions{
   246  				UserDataSource: "configmap/cm-userdata",
   247  				UserData:       userData,
   248  			}.ApplyDefaults(), time.Minute*5, nil)).To(Succeed())
   249  		})
   250  
   251  		AfterAll(func() {
   252  			deleteVM(vm)
   253  			controller.ConfigMaps().Delete("cm-userdata", nil)
   254  		})
   255  
   256  		It("Must be processed [Conformance]", func() {
   257  			ssh := waitSSH(vm)
   258  			Expect(framework.RunSimple(ssh, "cat", "/tmp/test-file1", "/tmp/test-file2")).To(Equal("Hello world!"))
   259  		})
   260  	})
   261  
   262  })