|
1 | 1 | package commands |
2 | 2 |
|
3 | | -// import ( |
4 | | -// "errors" |
5 | | -// "kool-dev/kool/core/builder" |
6 | | -// "kool-dev/kool/core/environment" |
7 | | -// "kool-dev/kool/services/cloud/setup" |
8 | | -// "os" |
9 | | -// "path/filepath" |
10 | | -// "strings" |
11 | | -// "testing" |
12 | | -// ) |
13 | | - |
14 | | -// func TestNewKoolDeploy(t *testing.T) { |
15 | | -// kd := NewKoolDeploy() |
16 | | - |
17 | | -// if _, is := kd.env.(*environment.DefaultEnvStorage); !is { |
18 | | -// t.Error("failed asserting default env storage") |
19 | | -// } |
20 | | - |
21 | | -// if _, is := kd.git.(*builder.DefaultCommand); !is { |
22 | | -// t.Error("failed asserting default git command") |
23 | | -// } |
24 | | -// } |
25 | | - |
26 | | -// func fakeKoolDeploy() *KoolDeploy { |
27 | | -// return &KoolDeploy{ |
28 | | -// *(newDefaultKoolService().Fake()), |
29 | | -// setup.NewDefaultCloudSetupParser(""), |
30 | | -// &KoolCloudDeployFlags{ |
31 | | -// DeployDomain: "foo", |
32 | | -// Token: "bar", |
33 | | -// }, |
34 | | -// environment.NewFakeEnvStorage(), |
35 | | -// &builder.FakeCommand{}, |
36 | | -// } |
37 | | -// } |
38 | | - |
39 | | -// func TestHandleDeployEnv(t *testing.T) { |
40 | | -// fake := fakeKoolDeploy() |
41 | | - |
42 | | -// files := []string{} |
43 | | - |
44 | | -// tmpDir := t.TempDir() |
45 | | -// fake.env.Set("PWD", tmpDir) |
46 | | - |
47 | | -// files = fake.handleDeployEnv(files) |
48 | | - |
49 | | -// if len(files) != 0 { |
50 | | -// t.Errorf("expected files to continue empty - no kool.deploy.env exists") |
51 | | -// } |
52 | | - |
53 | | -// if err := os.WriteFile(filepath.Join(tmpDir, "kool.deploy.env"), []byte("FOO=BAR"), os.ModePerm); err != nil { |
54 | | -// t.Fatal(err) |
55 | | -// } |
56 | | - |
57 | | -// files = fake.handleDeployEnv(files) |
58 | | - |
59 | | -// if len(files) != 1 { |
60 | | -// t.Errorf("expected files to have added kool.deploy.env") |
61 | | -// } |
62 | | - |
63 | | -// files = fake.handleDeployEnv(files) |
64 | | - |
65 | | -// if len(files) != 1 { |
66 | | -// t.Errorf("expected files to continue since was already there kool.deploy.env") |
67 | | -// } |
68 | | -// } |
69 | | - |
70 | | -// func TestValidate(t *testing.T) { |
71 | | -// fake := fakeKoolDeploy() |
72 | | - |
73 | | -// tmpDir := t.TempDir() |
74 | | -// fake.env.Set("PWD", tmpDir) |
75 | | - |
76 | | -// if err := fake.validate(); err == nil || !strings.Contains(err.Error(), "could not find required file") { |
77 | | -// t.Error("failed getting proper error out of validate when no kool.deploy.yml exists in current working directory") |
78 | | -// } |
79 | | - |
80 | | -// if err := os.WriteFile(filepath.Join(tmpDir, "kool.deploy.yml"), []byte("services:\n"), os.ModePerm); err != nil { |
81 | | -// t.Fatal(err) |
82 | | -// } |
83 | | - |
84 | | -// if err := fake.validate(); err != nil { |
85 | | -// t.Errorf("unexpcted error on validate when file exists: %v", err) |
86 | | -// } |
87 | | -// } |
88 | | - |
89 | | -// func TestParseFilesListFromGIT(t *testing.T) { |
90 | | -// fake := fakeKoolDeploy() |
91 | | - |
92 | | -// if files, err := fake.parseFilesListFromGIT([]string{}); err != nil { |
93 | | -// t.Errorf("unexpected error from parseFileListFromGIT: %v", err) |
94 | | -// } else if len(files) != 0 { |
95 | | -// t.Errorf("unexpected return of files: %#v", files) |
96 | | -// } |
97 | | - |
98 | | -// fake.git.(*builder.FakeCommand).MockExecOut = strings.Join([]string{"foo", string(rune(0x00)), "bar"}, "") |
99 | | - |
100 | | -// if files, err := fake.parseFilesListFromGIT([]string{}); err != nil { |
101 | | -// t.Errorf("unexpected error from parseFileListFromGIT: %v", err) |
102 | | -// } else if len(files) != 2 { |
103 | | -// t.Errorf("unexpected return of files: %#v", files) |
104 | | -// } |
105 | | - |
106 | | -// fake.git.(*builder.FakeCommand).MockExecError = errors.New("error") |
107 | | - |
108 | | -// if _, err := fake.parseFilesListFromGIT([]string{"foo", "bar"}); err == nil || !strings.Contains(err.Error(), "failed listing GIT") { |
109 | | -// t.Errorf("unexpected error from parseFileListFromGIT: %v", err) |
110 | | -// } |
111 | | -// } |
| 3 | +import ( |
| 4 | + "kool-dev/kool/core/environment" |
| 5 | + "kool-dev/kool/core/shell" |
| 6 | + "kool-dev/kool/services/cloud/setup" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +func TestNewKoolDeploy(t *testing.T) { |
| 14 | + kd := NewKoolDeploy(NewCloud()) |
| 15 | + |
| 16 | + if _, is := kd.env.(*environment.DefaultEnvStorage); !is { |
| 17 | + t.Error("failed asserting default env storage") |
| 18 | + } |
| 19 | + |
| 20 | + if _, is := kd.cloud.env.(*environment.DefaultEnvStorage); !is { |
| 21 | + t.Error("failed asserting default cloud.env storage") |
| 22 | + } |
| 23 | + |
| 24 | + if _, is := kd.setupParser.(*setup.DefaultCloudSetupParser); !is { |
| 25 | + t.Error("failed asserting default cloud setup parser") |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func fakeKoolDeploy() *KoolDeploy { |
| 30 | + c := NewCloud() |
| 31 | + c.Fake() |
| 32 | + return &KoolDeploy{ |
| 33 | + *(newDefaultKoolService().Fake()), |
| 34 | + c, |
| 35 | + setup.NewDefaultCloudSetupParser(""), |
| 36 | + &KoolCloudDeployFlags{}, |
| 37 | + environment.NewFakeEnvStorage(), |
| 38 | + nil, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestHandleDeployEnv(t *testing.T) { |
| 43 | + fake := fakeKoolDeploy() |
| 44 | + |
| 45 | + files := []string{} |
| 46 | + |
| 47 | + tmpDir := t.TempDir() |
| 48 | + fake.env.Set("PWD", tmpDir) |
| 49 | + |
| 50 | + files = fake.handleDeployEnv(files) |
| 51 | + |
| 52 | + if len(files) != 0 { |
| 53 | + t.Errorf("expected files to continue empty - no kool.deploy.env exists") |
| 54 | + } |
| 55 | + |
| 56 | + if err := os.WriteFile(filepath.Join(tmpDir, "kool.deploy.env"), []byte("FOO=BAR"), os.ModePerm); err != nil { |
| 57 | + t.Fatal(err) |
| 58 | + } |
| 59 | + |
| 60 | + files = fake.handleDeployEnv(files) |
| 61 | + |
| 62 | + if len(files) != 1 { |
| 63 | + t.Errorf("expected files to have added kool.deploy.env") |
| 64 | + } |
| 65 | + |
| 66 | + files = fake.handleDeployEnv(files) |
| 67 | + |
| 68 | + if len(files) != 1 { |
| 69 | + t.Errorf("expected files to continue since was already there kool.deploy.env") |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestCreateReleaseFile(t *testing.T) { |
| 74 | + fake := fakeKoolDeploy() |
| 75 | + |
| 76 | + tmpDir := t.TempDir() |
| 77 | + fake.env.Set("PWD", tmpDir) |
| 78 | + |
| 79 | + if _, err := fake.createReleaseFile(); err == nil || !strings.Contains(err.Error(), "no deploy config files found") { |
| 80 | + t.Errorf("expected error on createReleaseFile when no kool.deploy.yml exists in current working directory; got: %v", err) |
| 81 | + } |
| 82 | + |
| 83 | + mockConfig(tmpDir, t, nil) |
| 84 | + |
| 85 | + if tg, err := fake.createReleaseFile(); err != nil { |
| 86 | + t.Errorf("unexpected error on createReleaseFile; got: %v", err) |
| 87 | + } else if _, err := os.Stat(tg); err != nil { |
| 88 | + t.Errorf("expected tgz file to be created; got: %v", err) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestCleanupReleaseFile(t *testing.T) { |
| 93 | + fake := fakeKoolDeploy() |
| 94 | + |
| 95 | + tmpDir := t.TempDir() |
| 96 | + fake.env.Set("PWD", tmpDir) |
| 97 | + |
| 98 | + mockConfig(tmpDir, t, nil) |
| 99 | + |
| 100 | + f := filepath.Join(tmpDir, "kool.cloud.yml") |
| 101 | + fake.cleanupReleaseFile(f) |
| 102 | + if _, err := os.Stat(f); !os.IsNotExist(err) { |
| 103 | + t.Errorf("expected file to be removed") |
| 104 | + } |
| 105 | + |
| 106 | + fake.cleanupReleaseFile(f) |
| 107 | + if !fake.shell.(*shell.FakeShell).CalledError { |
| 108 | + t.Errorf("expected for Error to have been called on shell") |
| 109 | + } |
| 110 | + if !strings.Contains(fake.shell.(*shell.FakeShell).Err.Error(), "error trying to remove temporary tarball") { |
| 111 | + t.Errorf("expected to print proper error message if file removal fails") |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func TestLoadAndValidateConfig(t *testing.T) { |
| 116 | + fake := fakeKoolDeploy() |
| 117 | + |
| 118 | + tmpDir := t.TempDir() |
| 119 | + fake.env.Set("PWD", tmpDir) |
| 120 | + |
| 121 | + if err := fake.loadAndValidateConfig(); err == nil || !strings.Contains(err.Error(), "could not find required file") { |
| 122 | + t.Error("failed getting proper error out of loadAndValidateConfig when no kool.cloud.yml exists in current working directory") |
| 123 | + } |
| 124 | + |
| 125 | + mockConfig(tmpDir, t, []byte("services:\n\tfoo:\n")) |
| 126 | + |
| 127 | + if err := fake.loadAndValidateConfig(); err == nil || !strings.Contains(err.Error(), "found character that cannot start") { |
| 128 | + t.Errorf("unexpcted error on loadAndValidateConfig with bad config: %v", err) |
| 129 | + } |
| 130 | + |
| 131 | + mockConfig(tmpDir, t, nil) |
| 132 | + |
| 133 | + if err := fake.loadAndValidateConfig(); err != nil { |
| 134 | + t.Errorf("unexpcted error on loadAndValidateConfig when file exists: %v", err) |
| 135 | + } |
| 136 | + |
| 137 | + if fake.cloudConfig.Cloud.Services == nil { |
| 138 | + t.Error("failed loading cloud config") |
| 139 | + } |
| 140 | + |
| 141 | + if len(fake.cloudConfig.Cloud.Services) != 1 { |
| 142 | + t.Error("service count mismatch - should be 1") |
| 143 | + } else if *fake.cloudConfig.Cloud.Services["foo"].Image != "bar" { |
| 144 | + t.Error("failed loading service foo image 'bar'") |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +func mockConfig(tmpDir string, t *testing.T, mock []byte) { |
| 149 | + if mock == nil { |
| 150 | + mock = []byte("services:\n foo:\n image: bar\n") |
| 151 | + } |
| 152 | + |
| 153 | + if err := os.WriteFile(filepath.Join(tmpDir, "kool.cloud.yml"), mock, os.ModePerm); err != nil { |
| 154 | + t.Fatal(err) |
| 155 | + } |
| 156 | +} |
0 commit comments