-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain_test.go
122 lines (104 loc) · 3.42 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// rootfsbuilder - A simple tool to build Debian/Ubuntu rootfs tarballs
// Copyright (C) 2023 Hugo Melder
//
// SPDX-License-Identifier: MIT
//
package main
import (
"os"
"testing"
)
func getCwd() string {
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
return cwd
}
func validateValidConfig(t *testing.T, config *ConfigurationV1) {
if config.ConfigVersion != ConfigVersionV1 {
t.Errorf("expected config version to be %d, got: %d", ConfigVersionV1, config.ConfigVersion)
}
if config.Name != "test" {
t.Errorf("expected name to be 'test', got: %s", config.Name)
}
if config.Distribution != "debian" {
t.Errorf("expected distribution to be 'debian', got: %s", config.Distribution)
}
if config.Architecture != "arm64" {
t.Errorf("expected architecture to be 'arm64', got: %s", config.Architecture)
}
if config.Variant != "minbase" {
t.Errorf("expected variant to be 'minbase', got: %s", config.Variant)
}
if config.TarballType != "tar.gz" {
t.Errorf("expected tarball type to be 'tar.gz', got: %s", config.TarballType)
}
}
func TestParseConfiguration(t *testing.T) {
path := getCwd() + "/resources/testdata/valid_config.json"
config, err := parseConfiguration(path)
if err != nil {
t.Errorf("expected no parsing error, got: %s", err)
}
validateValidConfig(t, config)
}
func TestParseConfigurationInvalidVersion(t *testing.T) {
path := getCwd() + "/resources/testdata/invalid_version_config.json"
_, err := parseConfiguration(path)
if err == nil {
t.Error("expected error while parsing configuration with invalid version")
}
}
func TestParseConfigurationInvalidTarballType(t *testing.T) {
path := getCwd() + "/resources/testdata/invalid_tarball_type_config.json"
_, err := parseConfiguration(path)
if err == nil {
t.Error("expected error while parsing configuration with invalid tarball type")
}
}
func TestParseConfigurationInvalidPayloadType(t *testing.T) {
path := getCwd() + "/resources/testdata/invalid_payload_type_config.json"
_, err := parseConfiguration(path)
if err == nil {
t.Error("expected error while parsing configuration with invalid payload type")
}
}
func TestParseConfigurationMalformedJson(t *testing.T) {
path := getCwd() + "/resources/testdata/malformed_json_config.json"
_, err := parseConfiguration(path)
if err == nil {
t.Error("expected error while parsing configuration with malformed json")
}
}
func TestParseConfigurationMissingFields(t *testing.T) {
path := getCwd() + "/resources/testdata/missing_fields_config.json"
_, err := parseConfiguration(path)
if err == nil {
t.Error("expected error while parsing configuration with missing fields")
}
}
func TestParseConfigurationNonExistent(t *testing.T) {
path := getCwd() + "/resources/testdata/non_existent_config.json"
_, err := parseConfiguration(path)
if err == nil {
t.Error("expected error while parsing configuration with non existent file")
}
}
func TestProcessConfig(t *testing.T) {
path1 := getCwd() + "/resources/testdata/valid_config.json"
config1, err := processConfiguration([]string{path1})
if err != nil {
t.Errorf("expected no parsing error, got: %s", err)
}
if len(config1) != 1 {
t.Errorf("expected 1 configuration, got: %d", len(config1))
}
validateValidConfig(t, config1[0])
}
func TestProcessConfigWithDirectory(t *testing.T) {
_, err := processConfiguration([]string{getCwd() + "/resources/testdata"})
if err == nil {
t.Error("expected error while parsing configuration with directory")
}
}