Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(deployed_version): version from response headers and manual type #540

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 14 additions & 7 deletions service/deployed_version/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

dbtype "github.com/release-argus/Argus/db/types"
"github.com/release-argus/Argus/service/deployed_version/types/base"
"github.com/release-argus/Argus/service/deployed_version/types/manual"
"github.com/release-argus/Argus/service/deployed_version/types/web"
opt "github.com/release-argus/Argus/service/option"
"github.com/release-argus/Argus/service/status"
Expand All @@ -41,13 +42,6 @@ func TestMain(m *testing.M) {
}

func testLookup(lookupType string, failing bool) Lookup {
lookup, _ := New(
lookupType,
"yaml", "",
nil,
nil,
nil, nil)

// HardDefaults.
hardDefaults := &base.Defaults{}
hardDefaults.Default()
Expand All @@ -72,6 +66,13 @@ func testLookup(lookupType string, failing bool) Lookup {
test.StringPtr("http://example.com"),
)

lookup, _ := New(
lookupType,
"yaml", "",
options,
svcStatus,
defaults, hardDefaults)

switch l := lookup.(type) {
case *web.Lookup:
l.URL = test.LookupJSON["url_invalid"]
Expand All @@ -81,6 +82,12 @@ func testLookup(lookupType string, failing bool) Lookup {
options,
svcStatus,
defaults, hardDefaults)
case *manual.Lookup:
l.Version = "1.0.0"
l.Init(
options,
svcStatus,
defaults, hardDefaults)
}

return lookup
Expand Down
36 changes: 34 additions & 2 deletions service/deployed_version/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
// Returns: version, err.
func Refresh(
lookup Lookup,
previousType string,
overrides string,
semanticVersioning *string, // nil, "true", "false", "null" (unchanged, true, false, default).
) (string, error) {
Expand All @@ -47,7 +48,7 @@ func Refresh(
// semantic_versioning now resolves to a different value than the default.
(*semanticVersioning != "null" && *semanticVersioning == "true" != lookup.GetOptions().GetSemanticVersioning()))
// Whether we need to create a new Lookup.
usingOverrides := overrides != "" || semanticVerDiff
usingOverrides := (overrides != "" && previousType != "manual") || semanticVerDiff

newLookup := lookup
// Create a new Lookup if overrides provided.
Expand All @@ -61,6 +62,11 @@ func Refresh(
if err != nil {
return "", err
}
} else if previousType == "manual" && lookup.GetType() == "manual" &&
overrides != "" {
if err := json.Unmarshal([]byte(overrides), &lookup); err != nil {
return "", fmt.Errorf("failed to unmarshal deployedver.Lookup: %w", err)
}
}

// Log the lookup in use.
Expand All @@ -79,15 +85,41 @@ func Refresh(
return newLookup.GetStatus().DeployedVersion(), nil
}

// LookupTypeExtractor is a struct that extracts the type from a JSON string.
type LookupTypeExtractor struct {
Type string `json:"type"`
}

// applyOverridesJSON applies the JSON overrides to the Lookup.
func applyOverridesJSON(
lookup Lookup,
overrides string,
semanticVerDiff bool,
semanticVersioning *string, // nil, "true", "false", "null" (unchanged, true, false, default).
) (Lookup, error) {
var newLookup Lookup
var extractedOverrides *LookupTypeExtractor
if overrides != "" {
if err := json.Unmarshal([]byte(overrides), &extractedOverrides); err != nil {
return nil, fmt.Errorf("failed to unmarshal deployedver.Lookup: %w", err)
}
}
// Copy the existing Lookup.
newLookup := Copy(lookup)
if overrides == "" || (extractedOverrides.Type == "" || extractedOverrides.Type == lookup.GetType()) {
newLookup = Copy(lookup)
} else {
// Convert to the new type.
var err error
newLookup, err = New(
extractedOverrides.Type,
"yaml", "",
lookup.GetOptions(),
lookup.GetStatus(),
lookup.GetDefaults(), lookup.GetHardDefaults())
if err != nil {
return nil, err
}
}

// Apply the new semantic_versioning JSON value.
if semanticVerDiff {
Expand Down
81 changes: 62 additions & 19 deletions service/deployed_version/refresh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"
"time"

"github.com/release-argus/Argus/service/deployed_version/types/manual"
"github.com/release-argus/Argus/service/deployed_version/types/web"
"github.com/release-argus/Argus/service/status"
"github.com/release-argus/Argus/test"
Expand Down Expand Up @@ -57,6 +58,13 @@ func TestRefresh(t *testing.T) {
"nil Lookup": {
errRegex: `lookup is nil`,
},
"invalid JSON - manual": {
args: args{
overrides: test.StringPtr(`{`),
},
previous: testLookup("manual", false),
errRegex: `failed to unmarshal deployedver.Lookup`,
},
"Change of URL": {
args: args{
overrides: test.StringPtr(
Expand Down Expand Up @@ -123,7 +131,7 @@ func TestRefresh(t *testing.T) {
previous: testLookup("url", false),
errRegex: `^$`,
want: testVersion,
announce: 2,
announce: 1,
},
}

Expand All @@ -136,6 +144,8 @@ func TestRefresh(t *testing.T) {
switch l := tc.previous.(type) {
case *web.Lookup:
targetStatus = l.Status
case *manual.Lookup:
targetStatus = l.Status
}

// Copy the starting Status.
Expand All @@ -158,11 +168,15 @@ func TestRefresh(t *testing.T) {
}
previousStatus = targetStatus.Copy()
}
var previousType string
if tc.previous != nil {
previousType = tc.previous.GetType()
}

// WHEN we call Refresh.
got, err := Refresh(
tc.previous,
util.DereferenceOrDefault(tc.args.overrides),
previousType, util.DereferenceOrDefault(tc.args.overrides),
tc.args.semanticVersioning)

// THEN we get an error if expected.
Expand Down Expand Up @@ -223,7 +237,6 @@ func TestApplyOverridesJSON(t *testing.T) {
}
tests := map[string]struct {
args args
wantErr bool
errRegex string
}{
"no overrides, no semantic versioning change": {
Expand All @@ -233,7 +246,7 @@ func TestApplyOverridesJSON(t *testing.T) {
semanticVerDiff: false,
semanticVersioning: nil,
},
wantErr: false,
errRegex: `^$`,
},
"invalid semantic versioning JSON": {
args: args{
Expand All @@ -242,7 +255,6 @@ func TestApplyOverridesJSON(t *testing.T) {
semanticVerDiff: true,
semanticVersioning: test.StringPtr("invalid"),
},
wantErr: true,
errRegex: `failed to unmarshal deployedver\.Lookup\.SemanticVersioning`,
},
"valid semantic versioning change": {
Expand All @@ -252,7 +264,7 @@ func TestApplyOverridesJSON(t *testing.T) {
semanticVerDiff: true,
semanticVersioning: test.StringPtr("true"),
},
wantErr: false,
errRegex: `^$`,
},
"valid overrides JSON": {
args: args{
Expand All @@ -264,9 +276,9 @@ func TestApplyOverridesJSON(t *testing.T) {
semanticVerDiff: false,
semanticVersioning: nil,
},
wantErr: false,
errRegex: `^$`,
},
"invalid overrides JSON": {
"invalid overrides JSON - Invalid JSON": {
args: args{
lookup: testLookup("url", false),
overrides: test.StringPtr(
Expand All @@ -276,10 +288,9 @@ func TestApplyOverridesJSON(t *testing.T) {
semanticVerDiff: false,
semanticVersioning: nil,
},
wantErr: true,
errRegex: `failed to unmarshal deployedver.Lookup`,
},
"overrides in invalid format for url": {
"invalid overrides JSON - different var type": {
args: args{
lookup: testLookup("url", false),
overrides: test.StringPtr(
Expand All @@ -289,7 +300,6 @@ func TestApplyOverridesJSON(t *testing.T) {
semanticVerDiff: false,
semanticVersioning: nil,
},
wantErr: true,
errRegex: `^failed to unmarshal deployedver.Lookup`,
},
"overrides that fail CheckValues": {
Expand All @@ -302,9 +312,44 @@ func TestApplyOverridesJSON(t *testing.T) {
semanticVerDiff: false,
semanticVersioning: nil,
},
wantErr: true,
errRegex: `^url: <required>.*$`,
},
"change type with valid overrides - url to manual": {
args: args{
lookup: testLookup("url", false),
overrides: test.StringPtr(test.TrimJSON(`{
"type": "manual",
"version": "1.2.3"
}`)),
semanticVerDiff: false,
semanticVersioning: nil,
},
errRegex: `^$`,
},
"change type with valid overrides - manual to url": {
args: args{
lookup: testLookup("manual", false),
overrides: test.StringPtr(test.TrimJSON(`{
"type": "url",
"url": "` + test.LookupJSON["url_valid"] + `"
}`)),
semanticVerDiff: false,
semanticVersioning: nil,
},
errRegex: `^$`,
},
"change type to unknown type": {
args: args{
lookup: testLookup("url", false),
overrides: test.StringPtr(test.TrimJSON(`{
"type": "newType",
"url": []
}`)),
semanticVerDiff: false,
semanticVersioning: nil,
},
errRegex: `\stype: "newType" <invalid> \(expected one of \[url, manual\]\)$`,
},
}

for name, tc := range tests {
Expand All @@ -317,14 +362,12 @@ func TestApplyOverridesJSON(t *testing.T) {
tc.args.semanticVerDiff,
tc.args.semanticVersioning)

if (err != nil) != tc.wantErr {
t.Errorf("applyOverridesJSON() error = %v, wantErr %v", err, tc.wantErr)
return
}
if tc.wantErr && !util.RegexCheck(tc.errRegex, util.ErrorToString(err)) {
t.Errorf("applyOverridesJSON() error = %v, wantErr %v", err, tc.errRegex)
e := util.ErrorToString(err)
if !util.RegexCheck(tc.errRegex, util.ErrorToString(err)) {
t.Errorf("applyOverridesJSON() error mismatch\nwant match for:\n%q\ngot:\n%q",
tc.errRegex, e)
}
if !tc.wantErr && got == nil {
if tc.errRegex == `^$` && got == nil {
t.Errorf("applyOverridesJSON() got = nil, want non-nil")
}
})
Expand Down
Loading