1 package internal
2
3 import (
4 "os"
5 "testing"
6
7 "github.com/bazelbuild/rules_go/go/tools/bazel"
8 "github.com/stretchr/testify/assert"
9
10 "edge-infra.dev/pkg/lib/build/git"
11 "edge-infra.dev/pkg/lib/cli/sh"
12 )
13
14 func TestParseBuildInfo(t *testing.T) {
15
16 dir := bazel.TestTmpDir()
17 defer os.RemoveAll(dir)
18
19 repo := createTestGitRepo(t, dir)
20 assert.NoError(t, os.Chdir(repo))
21
22 g := git.NewInDir(repo)
23 commit, err := g.Commit()
24 assert.NoError(t, err)
25 timestamp, err := g.Timestamp()
26 assert.NoError(t, err)
27
28 tcs := map[string]struct {
29 packer Packer
30 valid bool
31 }{
32 "valid": {
33 Packer{
34 rc: true,
35 version: "7.7.7",
36 source: "https://github.com/ncrvoyix-swt-retail/edge-infra",
37 revision: commit,
38 created: timestamp.Unix(),
39 },
40 true,
41 },
42
43
44 "invalid version": {
45 Packer{
46 rc: true,
47 version: "abcde",
48 source: "https://github.com/ncrvoyix-swt-retail/edge-infra",
49 revision: commit,
50 created: timestamp.Unix(),
51 },
52 false,
53 },
54 "invalid source": {
55 Packer{
56 rc: true,
57 version: "7.7.7",
58 source: "not-a-real-url",
59 revision: commit,
60 created: timestamp.Unix(),
61 },
62 false,
63 },
64 "invalid revision": {
65 Packer{
66 rc: true,
67 version: "7.7.7",
68 source: "https://github.com/ncrvoyix-swt-retail/edge-infra",
69 revision: "!#$%@#$%&*()",
70 created: timestamp.Unix(),
71 },
72 false,
73 },
74 "negative created time handled as 0": {
75 Packer{
76 rc: true,
77 version: "7.7.7",
78 source: "https://github.com/ncrvoyix-swt-retail/edge-infra",
79 revision: commit,
80 created: -1,
81 },
82 true,
83 },
84 "multiple invalid components": {
85 Packer{
86 rc: true,
87 version: "abcde",
88 source: "not-a-real-url",
89 revision: commit,
90 created: 0,
91 },
92 false,
93 },
94
95
96 "revision is too short": {
97 Packer{
98 rc: true,
99 version: "7.7.7",
100 source: "https://github.com/ncrvoyix-swt-retail/edge-infra",
101 revision: "1",
102 created: timestamp.Unix(),
103 },
104 false,
105 },
106
107 "missing components are inferred": {
108 Packer{
109 rc: true,
110 version: "7.7.7",
111 },
112 true,
113 },
114 }
115
116 for test, tc := range tcs {
117 t.Run(test, func(t *testing.T) {
118 buildInfo, err := tc.packer.parseBuildInfo()
119 switch tc.valid {
120 case true:
121 assert.NoError(t, err)
122 assert.NotEmpty(t, buildInfo)
123 case false:
124 assert.Error(t, err)
125 assert.Empty(t, buildInfo)
126 }
127 })
128 }
129 }
130
131 func createTestGitRepo(t *testing.T, dir string) string {
132 t.Helper()
133
134 repo, err := os.MkdirTemp(dir, "git_test-*")
135 assert.NoError(t, err)
136 t.Log("test repo dir", repo)
137
138 shell := sh.NewInDir(repo)
139 for _, c := range []string{
140 "git init -q",
141 "git config user.name \"testbot\"",
142 "git config user.email \"testmail@testworld.co\"",
143 "git checkout -b test",
144
145 "git remote add origin git@github.com:testworld",
146 "git commit --allow-empty -m \"\"",
147 } {
148 assert.NoError(t, shell.RunE(c))
149 }
150
151 return repo
152 }
153
View as plain text