-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild_image_test.go
More file actions
174 lines (140 loc) · 5.97 KB
/
build_image_test.go
File metadata and controls
174 lines (140 loc) · 5.97 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
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0
package cmd_test
import (
"errors"
. "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"
)
var _ = Describe("BuildImageCmd", func() {
var (
c cmd.BuildImageCmd
opts cmd.BuildImageOpts
globalOpts *cmd.GlobalOptions
mockEnv *env.MockEnv
)
BeforeEach(func() {
mockEnv = env.NewMockEnv(GinkgoT())
globalOpts = &cmd.GlobalOptions{}
opts = cmd.BuildImageOpts{
GlobalOptions: globalOpts,
Dockerfile: "Dockerfile",
Package: "codesphere-vcodesphere-v1.66.0.tar.gz",
Registry: "my-registry.com/my-image",
}
c = cmd.BuildImageCmd{
Opts: opts,
Env: mockEnv,
}
})
AfterEach(func() {
mockEnv.AssertExpectations(GinkgoT())
})
Context("RunE method", func() {
It("calls BuildImage and fails when package manager fails", func() {
mockEnv.EXPECT().GetOmsWorkdir().Return("/test/workdir")
err := c.RunE(nil, []string{})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("failed to extract package"))
})
It("succeeds with valid options", func() {
mockEnv.EXPECT().GetOmsWorkdir().Return("/test/workdir")
// This will fail in real scenario because it tries to extract from real package
// But it should at least call the correct methods
err := c.RunE(nil, []string{})
Expect(err).To(HaveOccurred())
})
})
Context("BuildImage method", func() {
It("fails when package manager fails to get codesphere version", func() {
mockPackageManager := installer.NewMockPackageManager(GinkgoT())
mockImageManager := system.NewMockImageManager(GinkgoT())
mockPackageManager.EXPECT().Extract(false).Return(nil)
mockPackageManager.EXPECT().GetCodesphereVersion().Return("", errors.New("failed to extract version"))
err := c.BuildImage(mockPackageManager, mockImageManager)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("failed to get codesphere version from package"))
})
It("fails when image build fails", func() {
mockPackageManager := installer.NewMockPackageManager(GinkgoT())
mockImageManager := system.NewMockImageManager(GinkgoT())
c.Opts.Dockerfile = "Dockerfile"
c.Opts.Registry = "my-registry.com/my-image"
mockPackageManager.EXPECT().Extract(false).Return(nil)
mockPackageManager.EXPECT().GetCodesphereVersion().Return("codesphere-v1.66.0", nil)
mockImageManager.EXPECT().BuildImage("Dockerfile", "my-registry.com/my-image:codesphere-v1.66.0", ".").Return(errors.New("build failed"))
err := c.BuildImage(mockPackageManager, mockImageManager)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("failed to build image"))
})
It("fails when image push fails", func() {
mockPackageManager := installer.NewMockPackageManager(GinkgoT())
mockImageManager := system.NewMockImageManager(GinkgoT())
c.Opts.Dockerfile = "Dockerfile"
c.Opts.Registry = "my-registry.com/my-image"
mockPackageManager.EXPECT().Extract(false).Return(nil)
mockPackageManager.EXPECT().GetCodesphereVersion().Return("codesphere-v1.66.0", nil)
mockImageManager.EXPECT().BuildImage("Dockerfile", "my-registry.com/my-image:codesphere-v1.66.0", ".").Return(nil)
mockImageManager.EXPECT().PushImage("my-registry.com/my-image:codesphere-v1.66.0").Return(errors.New("push failed"))
err := c.BuildImage(mockPackageManager, mockImageManager)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("failed to push image"))
})
It("successfully builds and pushes image", func() {
mockPackageManager := installer.NewMockPackageManager(GinkgoT())
mockImageManager := system.NewMockImageManager(GinkgoT())
c.Opts.Dockerfile = "Dockerfile"
c.Opts.Registry = "my-registry.com/my-image"
mockPackageManager.EXPECT().Extract(false).Return(nil)
mockPackageManager.EXPECT().GetCodesphereVersion().Return("codesphere-v1.66.0", nil)
mockImageManager.EXPECT().BuildImage("Dockerfile", "my-registry.com/my-image:codesphere-v1.66.0", ".").Return(nil)
mockImageManager.EXPECT().PushImage("my-registry.com/my-image:codesphere-v1.66.0").Return(nil)
err := c.BuildImage(mockPackageManager, mockImageManager)
Expect(err).To(BeNil())
})
})
})
var _ = Describe("AddBuildImageCmd", func() {
var (
parentCmd *cobra.Command
globalOpts *cmd.GlobalOptions
)
BeforeEach(func() {
parentCmd = &cobra.Command{Use: "build"}
globalOpts = &cmd.GlobalOptions{}
})
It("adds the image command with correct properties and flags", func() {
cmd.AddBuildImageCmd(parentCmd, globalOpts)
var imageCmd *cobra.Command
for _, c := range parentCmd.Commands() {
if c.Use == "image" {
imageCmd = c
break
}
}
Expect(imageCmd).NotTo(BeNil())
Expect(imageCmd.Use).To(Equal("image"))
Expect(imageCmd.Short).To(Equal("Build and push Docker image using Dockerfile and Codesphere package version"))
Expect(imageCmd.Long).To(ContainSubstring("Build a Docker image from a Dockerfile and push it to a registry"))
Expect(imageCmd.Long).To(ContainSubstring("tagged with the Codesphere version"))
Expect(imageCmd.RunE).NotTo(BeNil())
// Check required flags
dockerfileFlag := imageCmd.Flags().Lookup("dockerfile")
Expect(dockerfileFlag).NotTo(BeNil())
Expect(dockerfileFlag.Shorthand).To(Equal("d"))
Expect(dockerfileFlag.Usage).To(ContainSubstring("Path to the Dockerfile to build"))
packageFlag := imageCmd.Flags().Lookup("package")
Expect(packageFlag).NotTo(BeNil())
Expect(packageFlag.Shorthand).To(Equal("p"))
Expect(packageFlag.Usage).To(ContainSubstring("Path to the Codesphere package"))
registryFlag := imageCmd.Flags().Lookup("registry")
Expect(registryFlag).NotTo(BeNil())
Expect(registryFlag.Shorthand).To(Equal("r"))
Expect(registryFlag.Usage).To(ContainSubstring("Registry URL to push to"))
})
})