-
Notifications
You must be signed in to change notification settings - Fork 0
/
license.go
89 lines (73 loc) · 2.26 KB
/
license.go
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
package integration
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/chef/automate/components/automate-cli/pkg/diagnostics"
)
type licenseSave struct {
LicenseID string `json:"license_id"`
}
type telemetryConfigResp struct {
LicenseID string `json:"license_id"`
}
// CreateLicenseDiagnostic create the diagnostic struct for the license service
// It will disabled telemetry. It verifies that telemetry is disabled, along with
// a license is present
func CreateLicenseDiagnostic() diagnostics.Diagnostic {
return diagnostics.Diagnostic{
Name: "license",
Generate: func(tstCtx diagnostics.TestContext) error {
currentConfig, err := telemetryConfig(tstCtx)
if err != nil {
return errors.Wrap(err, "Could not get current telemetry config")
}
tstCtx.SetValue("license", licenseSave{
LicenseID: currentConfig.LicenseID,
})
return nil
},
Verify: func(tstCtx diagnostics.VerificationTestContext) {
loaded := licenseSave{}
err := tstCtx.GetValue("license", &loaded)
require.NoError(tstCtx, err, "Generated context was not found")
resp, err := telemetryConfig(tstCtx)
require.NoError(tstCtx, err)
assert.Equal(tstCtx, loaded.LicenseID, resp.LicenseID, "Unexpected license id")
},
Cleanup: func(tstCtx diagnostics.TestContext) error {
loaded := licenseSave{}
err := tstCtx.GetValue("license", &loaded)
if err != nil {
return errors.Wrap(err, "Generated context was not found")
}
return err
},
}
}
func telemetryConfig(tstCtx diagnostics.TestContext) (*telemetryConfigResp, error) {
reqPath := fmt.Sprintf("/api/v0/telemetry/config")
resp, err := tstCtx.DoLBRequest(reqPath)
if err != nil {
return nil, errors.Wrapf(err, "Failed to GET %s", reqPath)
}
defer func() {
_ = resp.Body.Close()
}()
if resp.StatusCode != 200 {
return nil, errors.Errorf("Failed to GET %s with status code %d", reqPath, resp.StatusCode)
}
respUnmarshalled := telemetryConfigResp{}
err = json.NewDecoder(resp.Body).Decode(&respUnmarshalled)
if err != nil {
return nil, errors.Wrapf(err, "Failed to decode GET %s", reqPath)
}
return &respUnmarshalled, nil
}
func init() {
diagnostics.RegisterDiagnostic(
CreateLicenseDiagnostic(),
)
}