-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdate_oms_test.go
More file actions
101 lines (89 loc) · 3.06 KB
/
update_oms_test.go
File metadata and controls
101 lines (89 loc) · 3.06 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
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0
package cmd_test
import (
"embed"
"io"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/mock"
"github.com/codesphere-cloud/oms/cli/cmd"
"github.com/codesphere-cloud/oms/internal/portal"
"github.com/codesphere-cloud/oms/internal/version"
)
// I didn't find a good way to do this in memory without just reversing the code under test.
// While this is not ideal, at least it doesn't read from file during the test run but during compilation.
//
//go:generate mkdir -p testdata
//go:generate sh -c "echo fake-cli > testdata/oms-cli"
//go:generate sh -c "cd testdata && tar cfz testcli.tar.gz oms-cli"
//go:embed testdata
var testdata embed.FS
var _ = Describe("Update", func() {
var (
mockPortal *portal.MockPortal
mockVersion *version.MockVersion
mockUpdater *cmd.MockOMSUpdater
latestBuild portal.Build
buildToDownload portal.Build
c cmd.UpdateOmsCmd
)
BeforeEach(func() {
mockPortal = portal.NewMockPortal(GinkgoT())
mockVersion = version.NewMockVersion(GinkgoT())
mockUpdater = cmd.NewMockOMSUpdater(GinkgoT())
latestBuild = portal.Build{
Version: "0.0.42",
Artifacts: []portal.Artifact{
{Filename: "fakeos_fakearch.tar.gz"},
{Filename: "fakeos2_fakearch2.tar.gz"},
{Filename: "fakeos3_fakearch3.tar.gz"},
},
}
buildToDownload = portal.Build{
Version: "0.0.42",
Artifacts: []portal.Artifact{
{Filename: "fakeos_fakearch.tar.gz"},
},
}
c = cmd.UpdateOmsCmd{
Version: mockVersion,
Updater: mockUpdater,
}
})
Describe("SelfUpdate", func() {
It("Extracts oms-cli from the downloaded archive", func() {
mockVersion.EXPECT().Arch().Return("fakearch")
mockVersion.EXPECT().Version().Return("0.0.0")
mockVersion.EXPECT().Os().Return("fakeos")
mockPortal.EXPECT().GetBuild(portal.OmsProduct, "", "").Return(latestBuild, nil)
mockPortal.EXPECT().DownloadBuildArtifact(portal.OmsProduct, buildToDownload, mock.Anything, false).RunAndReturn(
func(product portal.Product, build portal.Build, file io.Writer, quiet bool) error {
embeddedFile, err := testdata.Open("testdata/testcli.tar.gz")
if err != nil {
Expect(err).NotTo(HaveOccurred())
}
defer func() { _ = embeddedFile.Close() }()
if _, err := io.Copy(file, embeddedFile); err != nil {
Expect(err).NotTo(HaveOccurred())
}
return nil
})
mockUpdater.EXPECT().Apply(mock.Anything).RunAndReturn(func(update io.Reader) error {
output, err := io.ReadAll(update)
Expect(err).NotTo(HaveOccurred())
// file content written in go:generate
Expect(string(output)).To(Equal("fake-cli\n"))
return nil
})
err := c.SelfUpdate(mockPortal)
Expect(err).NotTo(HaveOccurred())
})
It("Detects when current version is latest version", func() {
mockVersion.EXPECT().Version().Return(latestBuild.Version)
mockPortal.EXPECT().GetBuild(portal.OmsProduct, "", "").Return(latestBuild, nil)
err := c.SelfUpdate(mockPortal)
Expect(err).NotTo(HaveOccurred())
})
})
})