-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathextend_baseimage_test.go
More file actions
204 lines (166 loc) · 7.41 KB
/
extend_baseimage_test.go
File metadata and controls
204 lines (166 loc) · 7.41 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0
package cmd_test
import (
"errors"
"os"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/spf13/cobra"
"github.com/codesphere-cloud/oms/cli/cmd"
"github.com/codesphere-cloud/oms/internal/env"
"github.com/codesphere-cloud/oms/internal/installer"
"github.com/codesphere-cloud/oms/internal/system"
"github.com/codesphere-cloud/oms/internal/util"
)
var _ = Describe("ExtendBaseimageCmd", func() {
var (
c cmd.ExtendBaseimageCmd
opts *cmd.ExtendBaseimageOpts
globalOpts *cmd.GlobalOptions
mockEnv *env.MockEnv
)
BeforeEach(func() {
mockEnv = env.NewMockEnv(GinkgoT())
globalOpts = &cmd.GlobalOptions{}
opts = &cmd.ExtendBaseimageOpts{
GlobalOptions: globalOpts,
Dockerfile: "Dockerfile",
Force: false,
}
c = cmd.ExtendBaseimageCmd{
Opts: opts,
Env: mockEnv,
}
})
AfterEach(func() {
mockEnv.AssertExpectations(GinkgoT())
})
Context("RunE method", func() {
It("fails when package is empty", func() {
c.Opts.Package = ""
err := c.RunE(nil, []string{})
Expect(err).To(MatchError("required option package not set"))
})
It("calls GetOmsWorkdir and fails on package operations", func() {
c.Opts.Package = "non-existent-package.tar.gz"
mockEnv.EXPECT().GetOmsWorkdir().Return("/test/workdir")
err := c.RunE(nil, []string{})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("failed to extend baseimage"))
})
})
Context("ExtendBaseimage method", func() {
It("fails when package manager extraction fails", func() {
mockPackageManager := installer.NewMockPackageManager(GinkgoT())
mockImageManager := system.NewMockImageManager(GinkgoT())
mockPackageManager.EXPECT().Extract(false).Return(nil)
mockPackageManager.EXPECT().GetBaseimageName("").Return("", errors.New("failed to get image name: extraction failed"))
err := c.ExtendBaseimage(mockPackageManager, mockImageManager)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("failed to get image name"))
})
It("fails when config manager fails to extract OCI image index", func() {
mockPackageManager := installer.NewMockPackageManager(GinkgoT())
mockImageManager := system.NewMockImageManager(GinkgoT())
mockPackageManager.EXPECT().Extract(false).Return(nil)
mockPackageManager.EXPECT().GetBaseimageName("").Return("", errors.New("failed to extract OCI image index: index extraction failed"))
err := c.ExtendBaseimage(mockPackageManager, mockImageManager)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("failed to get image name"))
})
It("fails when OCI image index has no image names", func() {
mockPackageManager := installer.NewMockPackageManager(GinkgoT())
mockImageManager := system.NewMockImageManager(GinkgoT())
mockPackageManager.EXPECT().Extract(false).Return(nil)
mockPackageManager.EXPECT().GetBaseimageName("").Return("", errors.New("failed to read image tags: no image names found"))
err := c.ExtendBaseimage(mockPackageManager, mockImageManager)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("failed to get image name"))
})
It("fails when image manager fails to load image", func() {
mockPackageManager := installer.NewMockPackageManager(GinkgoT())
mockImageManager := system.NewMockImageManager(GinkgoT())
mockFileIO := util.NewMockFileIO(GinkgoT())
// Create a temporary file for the Dockerfile generation to work with
tempFile, err := os.CreateTemp("", "dockerfile-test-*")
Expect(err).To(BeNil())
defer func() { _ = os.Remove(tempFile.Name()) }()
defer func() { _ = tempFile.Close() }()
mockPackageManager.EXPECT().Extract(false).Return(nil)
mockPackageManager.EXPECT().GetBaseimageName("").Return("ubuntu:24.04-base", nil)
mockPackageManager.EXPECT().GetBaseimagePath("", false).Return("/test/workdir/deps/codesphere/images/workspace-agent-24.04.tar", nil)
mockPackageManager.EXPECT().FileIO().Return(mockFileIO)
mockFileIO.EXPECT().Create("Dockerfile").Return(tempFile, nil)
mockImageManager.EXPECT().LoadImage("/test/workdir/deps/codesphere/images/workspace-agent-24.04.tar").Return(errors.New("load failed"))
err = c.ExtendBaseimage(mockPackageManager, mockImageManager)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("failed to load baseimage file"))
})
It("uses force flag when extracting dependencies", func() {
mockPackageManager := installer.NewMockPackageManager(GinkgoT())
mockImageManager := system.NewMockImageManager(GinkgoT())
c.Opts.Force = true
mockPackageManager.EXPECT().Extract(true).Return(nil)
mockPackageManager.EXPECT().GetBaseimageName("").Return("", errors.New("failed to extract package to workdir: extraction failed"))
err := c.ExtendBaseimage(mockPackageManager, mockImageManager)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("failed to get image name"))
})
It("successfully completes workflow until dockerfile generation", func() {
mockPackageManager := installer.NewMockPackageManager(GinkgoT())
mockImageManager := system.NewMockImageManager(GinkgoT())
mockFileIO := util.NewMockFileIO(GinkgoT())
// Create a temporary file for the Dockerfile generation to work with
tempFile, err := os.CreateTemp("", "dockerfile-test-*")
Expect(err).To(BeNil())
defer func() { _ = os.Remove(tempFile.Name()) }()
defer func() { _ = tempFile.Close() }()
mockPackageManager.EXPECT().Extract(false).Return(nil)
mockPackageManager.EXPECT().GetBaseimageName("").Return("ubuntu:24.04-base", nil)
mockPackageManager.EXPECT().GetBaseimagePath("", false).Return("/test/workdir/deps/codesphere/images/workspace-agent-24.04.tar", nil)
mockPackageManager.EXPECT().FileIO().Return(mockFileIO)
mockFileIO.EXPECT().Create("Dockerfile").Return(tempFile, nil)
mockImageManager.EXPECT().LoadImage("/test/workdir/deps/codesphere/images/workspace-agent-24.04.tar").Return(nil)
err = c.ExtendBaseimage(mockPackageManager, mockImageManager)
Expect(err).To(BeNil())
})
})
})
var _ = Describe("AddExtendBaseimageCmd", func() {
var (
parentCmd *cobra.Command
globalOpts *cmd.GlobalOptions
)
BeforeEach(func() {
parentCmd = &cobra.Command{Use: "extend"}
globalOpts = &cmd.GlobalOptions{}
})
It("adds the baseimage command with correct properties and flags", func() {
cmd.AddExtendBaseimageCmd(parentCmd, globalOpts)
var baseimagCmd *cobra.Command
for _, c := range parentCmd.Commands() {
if c.Use == "baseimage" {
baseimagCmd = c
break
}
}
Expect(baseimagCmd).NotTo(BeNil())
Expect(baseimagCmd.Use).To(Equal("baseimage"))
Expect(baseimagCmd.Short).To(Equal("Extend Codesphere's workspace base image for customization"))
Expect(baseimagCmd.Long).To(ContainSubstring("Loads the baseimage from Codesphere package"))
Expect(baseimagCmd.RunE).NotTo(BeNil())
// Check flags
packageFlag := baseimagCmd.Flags().Lookup("package")
Expect(packageFlag).NotTo(BeNil())
Expect(packageFlag.Shorthand).To(Equal("p"))
dockerfileFlag := baseimagCmd.Flags().Lookup("dockerfile")
Expect(dockerfileFlag).NotTo(BeNil())
Expect(dockerfileFlag.Shorthand).To(Equal("d"))
Expect(dockerfileFlag.DefValue).To(Equal("Dockerfile"))
forceFlag := baseimagCmd.Flags().Lookup("force")
Expect(forceFlag).NotTo(BeNil())
Expect(forceFlag.Shorthand).To(Equal("f"))
Expect(forceFlag.DefValue).To(Equal("false"))
})
})