Skip to content

Commit be3ca23

Browse files
Merge pull request #27 from fabi200123/unit-tests
Adding unit-tests
2 parents 4213f35 + c5f2674 commit be3ca23

File tree

541 files changed

+232121
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

541 files changed

+232121
-4
lines changed

.github/workflows/go-tests.yml

+17
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,20 @@ jobs:
2525

2626
- name: Run GARM Go Tests
2727
run: make go-test
28+
29+
go-tests-windows:
30+
runs-on: windows-latest
31+
32+
steps:
33+
- name: Checkout
34+
uses: actions/checkout@v3
35+
36+
- name: Setup Golang
37+
uses: actions/setup-go@v3
38+
with:
39+
go-version-file: go.mod
40+
41+
- run: go version
42+
43+
- name: Run GARM Go Tests
44+
run: go test -v ./... -timeout=15m -parallel=4

cloudconfig/cloudconfig_test.go

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright 2023 Cloudbase Solutions SRL
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
// not use this file except in compliance with the License. You may obtain
5+
// a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
// License for the specific language governing permissions and limitations
13+
// under the License.
14+
15+
package cloudconfig
16+
17+
import (
18+
"encoding/base64"
19+
"fmt"
20+
"testing"
21+
22+
"github.com/cloudbase/garm-provider-common/defaults"
23+
"github.com/stretchr/testify/require"
24+
)
25+
26+
// helper function
27+
func getCloudInit() *CloudInit {
28+
return &CloudInit{
29+
PackageUpgrade: true,
30+
Packages: []string{"curl"},
31+
SSHAuthorizedKeys: []string{"test-ssh-key"},
32+
SystemInfo: &SystemInfo{
33+
DefaultUser: DefaultUser{
34+
Name: defaults.DefaultUser,
35+
Home: fmt.Sprintf("/home/%s", defaults.DefaultUser),
36+
Shell: defaults.DefaultUserShell,
37+
Groups: defaults.DefaultUserGroups,
38+
Sudo: "ALL=(ALL) NOPASSWD:ALL",
39+
},
40+
},
41+
WriteFiles: []File{
42+
{
43+
Encoding: "b64",
44+
Content: base64.StdEncoding.EncodeToString([]byte("test")),
45+
Owner: "test-owner",
46+
Path: "path",
47+
Permissions: "test-permissions",
48+
},
49+
},
50+
}
51+
}
52+
53+
var (
54+
cloudInit = getCloudInit()
55+
)
56+
57+
func TestNewDefaultCloudInitConfig(t *testing.T) {
58+
cloudInitCfg := NewDefaultCloudInitConfig()
59+
require.Equal(t, cloudInitCfg.PackageUpgrade, true)
60+
require.Equal(t, cloudInitCfg.Packages, []string{"curl", "tar"})
61+
require.Equal(t, cloudInitCfg.SystemInfo.DefaultUser.Name, defaults.DefaultUser)
62+
require.Equal(t, cloudInitCfg.SystemInfo.DefaultUser.Home, fmt.Sprintf("/home/%s", defaults.DefaultUser))
63+
require.Equal(t, cloudInitCfg.SystemInfo.DefaultUser.Shell, defaults.DefaultUserShell)
64+
require.Equal(t, cloudInitCfg.SystemInfo.DefaultUser.Groups, defaults.DefaultUserGroups)
65+
require.Equal(t, cloudInitCfg.SystemInfo.DefaultUser.Sudo, "ALL=(ALL) NOPASSWD:ALL")
66+
}
67+
68+
func TestAddCACertNil(t *testing.T) {
69+
err := cloudInit.AddCACert(nil)
70+
require.NoError(t, err)
71+
}
72+
73+
func TestAddCACertFailed(t *testing.T) {
74+
err := cloudInit.AddCACert([]byte("unknown-cert"))
75+
require.EqualError(t, err, "failed to parse CA cert bundle")
76+
}
77+
78+
func TestAddSSHKey(t *testing.T) {
79+
cloudInit.AddSSHKey(cloudInit.SSHAuthorizedKeys...)
80+
require.Equal(t, []string{"test-ssh-key"}, cloudInit.SSHAuthorizedKeys)
81+
}
82+
83+
func TestAddSSHKeyNotFound(t *testing.T) {
84+
cloudInit.AddSSHKey("new-test-ssh-key")
85+
require.Equal(t, []string{"test-ssh-key", "new-test-ssh-key"}, cloudInit.SSHAuthorizedKeys)
86+
}
87+
88+
func TestAddPackage(t *testing.T) {
89+
cloudInit.AddPackage(cloudInit.Packages...)
90+
require.Equal(t, []string{"curl"}, cloudInit.Packages)
91+
}
92+
93+
func TestAddPackageNotFound(t *testing.T) {
94+
cloudInit.AddPackage("tar")
95+
require.Equal(t, []string{"curl", "tar"}, cloudInit.Packages)
96+
}
97+
98+
func TestAddRunCmd(t *testing.T) {
99+
cloudInit.AddRunCmd("test-run-cmd")
100+
require.Equal(t, []string{"test-run-cmd"}, cloudInit.RunCmd)
101+
}
102+
103+
func TestAddFile(t *testing.T) {
104+
cloudInit.AddFile([]byte("test"), "test-path", "test-owner", "test-permissions")
105+
require.Equal(t, "b64", cloudInit.WriteFiles[0].Encoding)
106+
require.Equal(t, "dGVzdA==", cloudInit.WriteFiles[0].Content)
107+
require.Equal(t, "test-owner", cloudInit.WriteFiles[0].Owner)
108+
require.Equal(t, "test-permissions", cloudInit.WriteFiles[0].Permissions)
109+
}
110+
111+
func TestAddFilePath(t *testing.T) {
112+
cloudInit.AddFile([]byte("content"), "path", "test-owner", "test-permissions")
113+
}
114+
115+
func TestSerialize(t *testing.T) {
116+
serialized, err := cloudInit.Serialize()
117+
require.NoError(t, err)
118+
require.Contains(t, serialized, "#cloud-config")
119+
}

cloudconfig/templates_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2023 Cloudbase Solutions SRL
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
// not use this file except in compliance with the License. You may obtain
5+
// a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
// License for the specific language governing permissions and limitations
13+
// under the License.
14+
15+
package cloudconfig
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/require"
21+
)
22+
23+
func TestInstallRunnerScript(t *testing.T) {
24+
script, err := InstallRunnerScript(InstallRunnerParams{}, "linux", "test-template")
25+
require.NoError(t, err)
26+
require.Equal(t, "test-template", string(script))
27+
}
28+
29+
func TestInstallRunnerScriptParsingTemplateFailed(t *testing.T) {
30+
_, err := InstallRunnerScript(InstallRunnerParams{}, "linux", "{{ .test")
31+
require.Error(t, err)
32+
require.Contains(t, err.Error(), "parsing template: template: :1: unclosed action")
33+
}
34+
35+
func TestInstallRunnerScriptRenderingTemplateFailed(t *testing.T) {
36+
_, err := InstallRunnerScript(InstallRunnerParams{}, "linux", "{{ .test }}")
37+
require.Error(t, err)
38+
require.Contains(t, err.Error(), "rendering template: template: :1:3: executing")
39+
}

cloudconfig/util_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// Copyright 2023 Cloudbase Solutions SRL
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
// not use this file except in compliance with the License. You may obtain
5+
// a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
// License for the specific language governing permissions and limitations
13+
// under the License.
14+
115
package cloudconfig
216

317
import (

0 commit comments

Comments
 (0)