-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix cloudformation source deserialization
- Loading branch information
Showing
4 changed files
with
122 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,48 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// DeployCloudformationSource source type | ||
type DeployCloudformationSource int | ||
|
||
const ( | ||
// DeployCloudformationSourceUnknown default unknown | ||
DeployCloudformationSourceUnknown DeployCloudformationSource = iota | ||
// DeployCloudformationSourceText text | ||
DeployCloudformationSourceText | ||
// DeployCloudformationSourceArtifact artifact | ||
DeployCloudformationSourceArtifact | ||
) | ||
|
||
func (s DeployCloudformationSource) String() string { | ||
return [...]string{"unknown", "text", "artifact"}[s] | ||
} | ||
|
||
// ParseDeployCloudformationSource parse source | ||
func ParseDeployCloudformationSource(s string) (DeployCloudformationSource, error) { | ||
switch s { | ||
default: | ||
return DeployCloudformationSourceUnknown, fmt.Errorf("Unknown source %s", s) | ||
case "text": | ||
return DeployCloudformationSourceText, nil | ||
case "artifact": | ||
return DeployCloudformationSourceArtifact, nil | ||
} | ||
} | ||
|
||
// MarshalText source to text | ||
func (s DeployCloudformationSource) MarshalText() ([]byte, error) { | ||
return []byte(s.String()), nil | ||
} | ||
|
||
// UnmarshalText source from text | ||
func (s *DeployCloudformationSource) UnmarshalText(text []byte) error { | ||
parsedSource, err := ParseDeployCloudformationSource(string(text)) | ||
if err != nil { | ||
return err | ||
} | ||
*s = parsedSource | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,93 @@ | ||
package client | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/sergi/go-diff/diffmatchpatch" | ||
) | ||
|
||
var deployCloudformationStage DeployCloudformationStage | ||
|
||
func init() { | ||
deployCloudformationStage = *NewDeployCloudformationStage() | ||
deployCloudformationStage.Name = "New Deploy Cloudformation" | ||
} | ||
|
||
func TestDeployCloudformationStageGetName(t *testing.T) { | ||
name := "New Deploy Cloudformation" | ||
deployCloudformationStage.Name = name | ||
if deployCloudformationStage.GetName() != name { | ||
t.Fatalf("Deploy Cloudformation stage GetName() should be %s, not \"%s\"", name, deployCloudformationStage.GetName()) | ||
} | ||
} | ||
|
||
func TestDeployCloudformationStageGetType(t *testing.T) { | ||
if deployCloudformationStage.GetType() != DeployCloudformationStageType { | ||
t.Fatalf("Delete Manifest stage GetType() should be %s, not \"%s\"", DeployCloudformationStageType, deployCloudformationStage.GetType()) | ||
t.Fatalf("Deploy Cloudformation stage GetType() should be %s, not \"%s\"", DeployCloudformationStageType, deployCloudformationStage.GetType()) | ||
} | ||
if deployCloudformationStage.Type != DeployCloudformationStageType { | ||
t.Fatalf("Delete Manifest stage Type should be %s, not \"%s\"", DeployCloudformationStageType, deployCloudformationStage.Type) | ||
t.Fatalf("Deploy Cloudformation stage Type should be %s, not \"%s\"", DeployCloudformationStageType, deployCloudformationStage.Type) | ||
} | ||
} | ||
|
||
func TestDeployCloudformationStageSerialize(t *testing.T) { | ||
b, err := json.MarshalIndent(deployCloudformationStage, "", "\t") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
result := string(b) | ||
if result != deployCloudformationJSON { | ||
dmp := diffmatchpatch.New() | ||
diffs := dmp.DiffMain(deployCloudformationJSON, result, true) | ||
t.Fatalf("Deploy Cloudformation not as expected: %s", dmp.DiffPrettyText(diffs)) | ||
} | ||
} | ||
|
||
func TestDeployCloudformationStageDeserialize(t *testing.T) { | ||
var stageMap map[string]interface{} | ||
err := json.Unmarshal([]byte(deployCloudformationJSON), &stageMap) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
stageInterface, err := parseDeployCloudformationStage(stageMap) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
stage := stageInterface.(*DeployCloudformationStage) | ||
if stage.Source != DeployCloudformationSourceText { | ||
t.Fatalf("Should have source type text") | ||
} | ||
} | ||
|
||
var deployCloudformationJSON = `{ | ||
"name": "New Deploy Cloudformation", | ||
"refId": "", | ||
"type": "deployCloudFormation", | ||
"requisiteStageRefIds": [], | ||
"sendNotifications": false, | ||
"stageEnabled": null, | ||
"completeOtherBranchesThenFail": false, | ||
"continuePipeline": false, | ||
"failOnFailedExpressions": false, | ||
"failPipeline": true, | ||
"overrideTimeout": false, | ||
"restrictExecutionDuringTimeWindow": false, | ||
"restrictedExecutionWindow": null, | ||
"notifications": null, | ||
"expectedArtifacts": [], | ||
"requiredArtifactIds": [], | ||
"actionOnReplacement": "", | ||
"capabilities": null, | ||
"changeSetName": "", | ||
"credentials": "", | ||
"executeChangeSet": false, | ||
"isChangeSet": false, | ||
"parameters": null, | ||
"regions": null, | ||
"roleARN": "", | ||
"source": "text", | ||
"stackArtifact": null, | ||
"stackName": "", | ||
"tags": null, | ||
"templateBody": null | ||
}` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters