-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathapi_int_test.go
More file actions
132 lines (109 loc) · 4.21 KB
/
api_int_test.go
File metadata and controls
132 lines (109 loc) · 4.21 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
package integration
import (
"path/filepath"
"strings"
"testing"
"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/fileutils"
"github.com/ActiveState/cli/internal/testhelpers/e2e"
"github.com/ActiveState/cli/internal/testhelpers/suite"
"github.com/ActiveState/cli/internal/testhelpers/tagsuite"
)
type ApiIntegrationTestSuite struct {
tagsuite.Suite
}
func (suite *ApiIntegrationTestSuite) TestRequestHeaders() {
suite.OnlyRunForTags(tagsuite.Critical)
ts := e2e.New(suite.T(), false)
defer ts.Close()
cp := ts.SpawnWithOpts(
e2e.OptArgs("checkout", "ActiveState-CLI/Empty", "."),
e2e.OptAppendEnv(constants.DebugServiceRequestsEnvVarName+"=true", "VERBOSE=true"),
)
// e.g. User-Agent: state/0.38.0-SHA0deadbeef0; release (Windows; 10.0.22621; x86_64)
cp.ExpectRe(`User-Agent: state/(\d+\.?)+-SHA[[:xdigit:]]+; \S+ \([^;]+; [^;]+; [^)]+\)`)
cp.ExpectRe(`X-Requestor: [[:xdigit:]-]+`) // UUID
cp.ExpectExitCode(0)
}
// TestNoApiCallsForPlainInvocation asserts that a bare `state` does not make any API calls.
func (suite *ApiIntegrationTestSuite) TestNoApiCallsForPlainInvocation() {
suite.OnlyRunForTags(tagsuite.Critical)
ts := e2e.New(suite.T(), false)
defer ts.Close()
cp := ts.SpawnWithOpts(
e2e.OptAppendEnv(constants.DebugServiceRequestsEnvVarName + "=true"),
)
cp.ExpectExitCode(0)
readLogFile := false
for _, path := range ts.LogFiles() {
if !strings.HasPrefix(filepath.Base(path), "state-") {
continue
}
contents := string(fileutils.ReadFileUnsafe(path))
suite.Assert().NotContains(contents, "URL: ") // pkg/platform/api logs URL, User-Agent, and X-Requestor for API calls
readLogFile = true
}
suite.Assert().True(readLogFile, "did not read log file")
}
func (suite *ApiIntegrationTestSuite) TestAPIHostConfig_SetBeforeInvocation() {
suite.OnlyRunForTags(tagsuite.Critical)
ts := e2e.New(suite.T(), false)
defer ts.Close()
ts.SetConfig("api.host", "test.example.com")
cp := ts.SpawnWithOpts(
e2e.OptArgs("checkout", "doesnt/exist"),
e2e.OptAppendEnv("VERBOSE=true"),
)
cp.ExpectExitCode(11) // We know this command will fail, but we want to check the log file
ts.IgnoreLogErrors()
correctHostCount := 0
incorrectHostCount := 0
for _, path := range ts.LogFiles() {
contents := string(fileutils.ReadFileUnsafe(path))
if strings.Contains(contents, "test.example.com") {
correctHostCount++
}
if strings.Contains(contents, "platform.activestate.com") {
incorrectHostCount++
}
}
suite.Assert().Greater(correctHostCount, 0, "Log file should contain the configured API host 'test.example.com'")
suite.Assert().Equal(incorrectHostCount, 0, "Log file should not contain the default API host 'platform.activestate.com'")
// Clean up - remove the config setting
cp = ts.Spawn("config", "set", "api.host", "")
cp.Expect("Successfully")
cp.ExpectExitCode(0)
}
func (suite *ApiIntegrationTestSuite) TestAPIHostConfig_SetOnFirstInvocation() {
suite.OnlyRunForTags(tagsuite.Critical)
ts := e2e.New(suite.T(), false)
defer ts.Close()
cp := ts.Spawn("config", "set", "api.host", "test.example.com")
cp.Expect("Successfully")
cp.ExpectExitCode(0)
cp = ts.SpawnWithOpts(
e2e.OptArgs("checkout", "doesnt/exist"),
e2e.OptAppendEnv("VERBOSE=true"),
)
cp.ExpectExitCode(11) // We know this command will fail, but we want to check the log file
ts.IgnoreLogErrors()
// Because the config value is set on first invocation of the state tool the state-svc will start
// before the state tool has a chance to set the host in the config. This means that it will still
// use the default host. The above test confirms that the service will use the configured host if
// the config is set before the state tool is invoked.
correctHostCount := 0
for _, path := range ts.LogFiles() {
contents := string(fileutils.ReadFileUnsafe(path))
if strings.Contains(contents, "test.example.com") {
correctHostCount++
}
}
suite.Assert().Greater(correctHostCount, 0, "Log file should contain the configured API host 'test.example.com'")
// Clean up - remove the config setting
cp = ts.Spawn("config", "set", "api.host", "")
cp.Expect("Successfully")
cp.ExpectExitCode(0)
}
func TestApiIntegrationTestSuite(t *testing.T) {
suite.Run(t, new(ApiIntegrationTestSuite))
}