Skip to content

Commit

Permalink
enhance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vpnachev committed Jun 13, 2020
1 parent a0160bf commit 9dd9612
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 37 deletions.
100 changes: 92 additions & 8 deletions pkg/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,107 @@ import (
. "github.com/onsi/gomega"
)

var (
permissions = int32(0644)
unit1 = "unit1"
unit2 = "unit2"
dropin = "dropin"
filePath = "/var/lib/kubelet/ca.crt"

unitContent = []byte(`[Unit]
Description=test content
[Install]
WantedBy=multi-user.target
[Service]
Restart=always`)
dropinContent = []byte(`[Service]
Environment="DOCKER_OPTS=--log-opt max-size=60m --log-opt max-file=3"`)

fileContent = []byte(`secretRef:
name: default-token-d9nzl
dataKey: token`)

criConfig = v1alpha1.CRIConfig{
Name: v1alpha1.CRINameContainerD,
}

osc = commongen.OperatingSystemConfig{
Units: []*commongen.Unit{
{
Name: unit1,
Content: unitContent,
},
{
Name: unit2,
Content: unitContent,
DropIns: []*commongen.DropIn{
{
Name: dropin,
Content: dropinContent,
},
},
},
},
Files: []*commongen.File{
{
Path: filePath,
Content: fileContent,
Permissions: &permissions,
},
},
}
)

var _ = Describe("Garden Linux OS Generator Test", func() {

Describe("Conformance Tests", func() {
var box = packr.NewBox("./testfiles")
g := gardenlinux_generator.CloudInitGenerator()
test.DescribeTest(gardenlinux_generator.CloudInitGenerator(), box)()

It("should render correctly with Containerd enabled", func() {
expectedCloudInit, err := box.Find("cloud-init-containerd")
It("[docker] [bootstrap] should render correctly ", func() {
expectedCloudInit, err := box.Find("docker-bootstrap")
Expect(err).NotTo(HaveOccurred())

cloudInit, _, err := g.Generate(
&commongen.OperatingSystemConfig{
CRI: &v1alpha1.CRIConfig{
Name: v1alpha1.CRINameContainerD,
},
})
osc.Bootstrap = true
osc.CRI = nil
cloudInit, _, err := g.Generate(&osc)

Expect(err).NotTo(HaveOccurred())
Expect(cloudInit).To(Equal(expectedCloudInit))
})

It("[docker] [reconcile] should render correctly", func() {
expectedCloudInit, err := box.Find("docker-reconcile")
Expect(err).NotTo(HaveOccurred())

osc.Bootstrap = false
osc.CRI = nil
cloudInit, _, err := g.Generate(&osc)

Expect(err).NotTo(HaveOccurred())
Expect(cloudInit).To(Equal(expectedCloudInit))
})

It("[containerd] [bootstrap] should render correctly", func() {
expectedCloudInit, err := box.Find("containerd-bootstrap")
Expect(err).NotTo(HaveOccurred())

osc.Bootstrap = true
osc.CRI = &criConfig
cloudInit, _, err := g.Generate(&osc)

Expect(err).NotTo(HaveOccurred())
Expect(cloudInit).To(Equal(expectedCloudInit))
})

It("[containerd] [reconcile] should render correctly bootstrap", func() {
expectedCloudInit, err := box.Find("containerd-reconcile")
Expect(err).NotTo(HaveOccurred())

osc.Bootstrap = false
osc.CRI = &criConfig
cloudInit, _, err := g.Generate(&osc)

Expect(err).NotTo(HaveOccurred())
Expect(cloudInit).To(Equal(expectedCloudInit))
Expand Down
26 changes: 12 additions & 14 deletions pkg/generator/templates/cloud-init.gardenlinux.template
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
#!/bin/bash

{{ define "put-content" -}}
{{- define "put-content" -}}
cat << EOF | base64 -d > '{{ .Path }}'
{{ .Content }}
EOF
{{- end -}}

{{ if isContainerDEnabled .CRI -}}
{{ if and (isContainerDEnabled .CRI) .Bootstrap }}
mkdir -p /etc/systemd/system/containerd.service.d
cat <<EOF > /etc/systemd/system/containerd.service.d/11-exec_config.conf
[Service]
ExecStart=
ExecStart=/usr/bin/containerd --config=/etc/containerd/config.toml
EOF
chmod 0644 /etc/systemd/system/containerd.service.d/11-exec_config.conf
{{ end -}}
{{- end }}

{{- range $_, $file := .Files -}}
{{ range $_, $file := .Files -}}
mkdir -p '{{ $file.Dirname }}'
{{ template "put-content" $file }}
{{- if $file.Permissions }}
{{ if $file.Permissions -}}
chmod '{{ $file.Permissions }}' '{{ $file.Path }}'
{{ end }}
{{ end }}

{{- range $_, $unit := .Units -}}
{{ range $_, $unit := .Units -}}
{{ if $unit.Content -}}
{{ template "put-content" $unit }}
{{- end }}
Expand All @@ -39,15 +39,13 @@ mkdir -p '{{ $unit.DropIns.Path }}'
grep -sq "^nfsd$" /etc/modules || echo "nfsd" >>/etc/modules
modprobe nfsd
systemctl daemon-reload
{{ if .Bootstrap }}
{{ if isContainerDEnabled .CRI -}}
{{- if .Bootstrap }}
{{- if isContainerDEnabled .CRI }}
systemctl enable containerd && systemctl restart containerd
{{- else -}}
{{- end }}
systemctl enable docker && systemctl restart docker
{{ end -}}
{{ end -}}

{{- end }}

{{ range $_, $unit := .Units -}}
{{- range $_, $unit := .Units }}
systemctl enable '{{ $unit.Name }}' && systemctl restart '{{ $unit.Name }}'
{{ end }}
{{- end }}
4 changes: 2 additions & 2 deletions pkg/generator/testfiles/cloud-init
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ YmFy
EOF
chmod '0600' '/foo'



cat << EOF | base64 -d > '/etc/systemd/system/docker.service'
dW5pdA==
EOF
Expand All @@ -18,7 +20,5 @@ EOF
grep -sq "^nfsd$" /etc/modules || echo "nfsd" >>/etc/modules
modprobe nfsd
systemctl daemon-reload

systemctl enable docker && systemctl restart docker
systemctl enable 'docker.service' && systemctl restart 'docker.service'

13 changes: 0 additions & 13 deletions pkg/generator/testfiles/cloud-init-containerd

This file was deleted.

36 changes: 36 additions & 0 deletions pkg/generator/testfiles/containerd-bootstrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash
mkdir -p /etc/systemd/system/containerd.service.d
cat <<EOF > /etc/systemd/system/containerd.service.d/11-exec_config.conf
[Service]
ExecStart=
ExecStart=/usr/bin/containerd --config=/etc/containerd/config.toml
EOF
chmod 0644 /etc/systemd/system/containerd.service.d/11-exec_config.conf

mkdir -p '/var/lib/kubelet'
cat << EOF | base64 -d > '/var/lib/kubelet/ca.crt'
c2VjcmV0UmVmOgpuYW1lOiBkZWZhdWx0LXRva2VuLWQ5bnpsCmRhdGFLZXk6IHRva2Vu
EOF
chmod '0644' '/var/lib/kubelet/ca.crt'



cat << EOF | base64 -d > '/etc/systemd/system/unit1'
W1VuaXRdCkRlc2NyaXB0aW9uPXRlc3QgY29udGVudApbSW5zdGFsbF0KV2FudGVkQnk9bXVsdGktdXNlci50YXJnZXQKW1NlcnZpY2VdClJlc3RhcnQ9YWx3YXlz
EOF
cat << EOF | base64 -d > '/etc/systemd/system/unit2'
W1VuaXRdCkRlc2NyaXB0aW9uPXRlc3QgY29udGVudApbSW5zdGFsbF0KV2FudGVkQnk9bXVsdGktdXNlci50YXJnZXQKW1NlcnZpY2VdClJlc3RhcnQ9YWx3YXlz
EOF

mkdir -p '/etc/systemd/system/unit2.d'
cat << EOF | base64 -d > '/etc/systemd/system/unit2.d/dropin'
W1NlcnZpY2VdCkVudmlyb25tZW50PSJET0NLRVJfT1BUUz0tLWxvZy1vcHQgbWF4LXNpemU9NjBtIC0tbG9nLW9wdCBtYXgtZmlsZT0zIg==
EOF

grep -sq "^nfsd$" /etc/modules || echo "nfsd" >>/etc/modules
modprobe nfsd
systemctl daemon-reload
systemctl enable containerd && systemctl restart containerd
systemctl enable docker && systemctl restart docker
systemctl enable 'unit1' && systemctl restart 'unit1'
systemctl enable 'unit2' && systemctl restart 'unit2'
27 changes: 27 additions & 0 deletions pkg/generator/testfiles/containerd-reconcile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

mkdir -p '/var/lib/kubelet'
cat << EOF | base64 -d > '/var/lib/kubelet/ca.crt'
c2VjcmV0UmVmOgpuYW1lOiBkZWZhdWx0LXRva2VuLWQ5bnpsCmRhdGFLZXk6IHRva2Vu
EOF
chmod '0644' '/var/lib/kubelet/ca.crt'



cat << EOF | base64 -d > '/etc/systemd/system/unit1'
W1VuaXRdCkRlc2NyaXB0aW9uPXRlc3QgY29udGVudApbSW5zdGFsbF0KV2FudGVkQnk9bXVsdGktdXNlci50YXJnZXQKW1NlcnZpY2VdClJlc3RhcnQ9YWx3YXlz
EOF
cat << EOF | base64 -d > '/etc/systemd/system/unit2'
W1VuaXRdCkRlc2NyaXB0aW9uPXRlc3QgY29udGVudApbSW5zdGFsbF0KV2FudGVkQnk9bXVsdGktdXNlci50YXJnZXQKW1NlcnZpY2VdClJlc3RhcnQ9YWx3YXlz
EOF

mkdir -p '/etc/systemd/system/unit2.d'
cat << EOF | base64 -d > '/etc/systemd/system/unit2.d/dropin'
W1NlcnZpY2VdCkVudmlyb25tZW50PSJET0NLRVJfT1BUUz0tLWxvZy1vcHQgbWF4LXNpemU9NjBtIC0tbG9nLW9wdCBtYXgtZmlsZT0zIg==
EOF

grep -sq "^nfsd$" /etc/modules || echo "nfsd" >>/etc/modules
modprobe nfsd
systemctl daemon-reload
systemctl enable 'unit1' && systemctl restart 'unit1'
systemctl enable 'unit2' && systemctl restart 'unit2'
28 changes: 28 additions & 0 deletions pkg/generator/testfiles/docker-bootstrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

mkdir -p '/var/lib/kubelet'
cat << EOF | base64 -d > '/var/lib/kubelet/ca.crt'
c2VjcmV0UmVmOgpuYW1lOiBkZWZhdWx0LXRva2VuLWQ5bnpsCmRhdGFLZXk6IHRva2Vu
EOF
chmod '0644' '/var/lib/kubelet/ca.crt'



cat << EOF | base64 -d > '/etc/systemd/system/unit1'
W1VuaXRdCkRlc2NyaXB0aW9uPXRlc3QgY29udGVudApbSW5zdGFsbF0KV2FudGVkQnk9bXVsdGktdXNlci50YXJnZXQKW1NlcnZpY2VdClJlc3RhcnQ9YWx3YXlz
EOF
cat << EOF | base64 -d > '/etc/systemd/system/unit2'
W1VuaXRdCkRlc2NyaXB0aW9uPXRlc3QgY29udGVudApbSW5zdGFsbF0KV2FudGVkQnk9bXVsdGktdXNlci50YXJnZXQKW1NlcnZpY2VdClJlc3RhcnQ9YWx3YXlz
EOF

mkdir -p '/etc/systemd/system/unit2.d'
cat << EOF | base64 -d > '/etc/systemd/system/unit2.d/dropin'
W1NlcnZpY2VdCkVudmlyb25tZW50PSJET0NLRVJfT1BUUz0tLWxvZy1vcHQgbWF4LXNpemU9NjBtIC0tbG9nLW9wdCBtYXgtZmlsZT0zIg==
EOF

grep -sq "^nfsd$" /etc/modules || echo "nfsd" >>/etc/modules
modprobe nfsd
systemctl daemon-reload
systemctl enable docker && systemctl restart docker
systemctl enable 'unit1' && systemctl restart 'unit1'
systemctl enable 'unit2' && systemctl restart 'unit2'
27 changes: 27 additions & 0 deletions pkg/generator/testfiles/docker-reconcile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

mkdir -p '/var/lib/kubelet'
cat << EOF | base64 -d > '/var/lib/kubelet/ca.crt'
c2VjcmV0UmVmOgpuYW1lOiBkZWZhdWx0LXRva2VuLWQ5bnpsCmRhdGFLZXk6IHRva2Vu
EOF
chmod '0644' '/var/lib/kubelet/ca.crt'



cat << EOF | base64 -d > '/etc/systemd/system/unit1'
W1VuaXRdCkRlc2NyaXB0aW9uPXRlc3QgY29udGVudApbSW5zdGFsbF0KV2FudGVkQnk9bXVsdGktdXNlci50YXJnZXQKW1NlcnZpY2VdClJlc3RhcnQ9YWx3YXlz
EOF
cat << EOF | base64 -d > '/etc/systemd/system/unit2'
W1VuaXRdCkRlc2NyaXB0aW9uPXRlc3QgY29udGVudApbSW5zdGFsbF0KV2FudGVkQnk9bXVsdGktdXNlci50YXJnZXQKW1NlcnZpY2VdClJlc3RhcnQ9YWx3YXlz
EOF

mkdir -p '/etc/systemd/system/unit2.d'
cat << EOF | base64 -d > '/etc/systemd/system/unit2.d/dropin'
W1NlcnZpY2VdCkVudmlyb25tZW50PSJET0NLRVJfT1BUUz0tLWxvZy1vcHQgbWF4LXNpemU9NjBtIC0tbG9nLW9wdCBtYXgtZmlsZT0zIg==
EOF

grep -sq "^nfsd$" /etc/modules || echo "nfsd" >>/etc/modules
modprobe nfsd
systemctl daemon-reload
systemctl enable 'unit1' && systemctl restart 'unit1'
systemctl enable 'unit2' && systemctl restart 'unit2'

0 comments on commit 9dd9612

Please sign in to comment.