This repository has been archived by the owner on Sep 24, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Chuck Ha <chuckh@vmware.com>
- Loading branch information
Showing
6 changed files
with
192 additions
and
88 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
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
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cloudinit | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/pkg/errors" | ||
"sigs.k8s.io/kind/pkg/exec" | ||
) | ||
|
||
type unknown struct { | ||
module string | ||
lines []string | ||
} | ||
|
||
func newUnknown(module string) action { | ||
return &unknown{module: module} | ||
} | ||
|
||
// Unmarshal will unmarshall unknown actions and slurp the value | ||
func (u *unknown) Unmarshal(data []byte) error { | ||
// try unmarshalling to a slice of strings | ||
var s1 []string | ||
if err := json.Unmarshal(data, &s1); err != nil { | ||
if _, ok := err.(*json.UnmarshalTypeError); !ok { | ||
return errors.WithStack(err) | ||
} | ||
} else { | ||
u.lines = s1 | ||
return nil | ||
} | ||
|
||
// If it's not a slice of strings it should be one string value | ||
var s2 string | ||
if err := json.Unmarshal(data, &s2); err != nil { | ||
return errors.WithStack(err) | ||
} | ||
|
||
u.lines = []string{s2} | ||
return nil | ||
} | ||
|
||
// Run will do nothing since the cloud config module is unknown. | ||
func (u *unknown) Run(_ exec.Cmder) ([]string, error) { | ||
return u.lines, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cloudinit | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestUnknown_Run(t *testing.T) { | ||
u := &unknown{ | ||
lines: []string{}, | ||
} | ||
lines, err := u.Run(nil) | ||
if err != nil { | ||
t.Fatal("err should always be nil") | ||
} | ||
if len(lines) != 0 { | ||
t.Fatalf("return exactly what was parsed. did not expect anything here but got: %v", lines) | ||
} | ||
} | ||
|
||
func TestUnknown_Unmarshal(t *testing.T) { | ||
u := &unknown{} | ||
expected := []string{"test 1", "test 2", "test 3"} | ||
input := `["test 1", "test 2", "test 3"]` | ||
if err := u.Unmarshal([]byte(input)); err != nil { | ||
t.Fatalf("should not return an error but got: %v", err) | ||
} | ||
if len(u.lines) != len(expected) { | ||
t.Fatalf("expected exactly %v lines but got %v", 3, u.lines) | ||
} | ||
if !reflect.DeepEqual(u.lines, expected) { | ||
t.Fatalf("lines should be %v but it is %v", expected, u.lines) | ||
} | ||
} |
Oops, something went wrong.