forked from google/go-tpm-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Joshua Krstic <[email protected]>
- Loading branch information
1 parent
12c2c65
commit c2f108f
Showing
7 changed files
with
136 additions
and
0 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,42 @@ | ||
// Package experiments contains functionalities to retrieve synced experiments | ||
package experiments | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
// Experiments contains the experiments flags this version of the launcher expects to receive. | ||
// Failure to unmarshal the experiment JSON data will result in an empty object being returned | ||
// to treat experiment flags as their default value. The error should still be checked. | ||
type Experiments struct { | ||
EnableTestFeatureForImage bool | ||
EnableSignedContainerImage bool | ||
} | ||
|
||
// New takes a filepath, opens the file, and calls ReadJsonInput with the contents | ||
// of the file. | ||
// If the file cannot be opened, the experiments map is set to an empty map. | ||
func New(fpath string) (Experiments, error) { | ||
f, err := os.ReadFile(fpath) | ||
if err != nil { | ||
// Return default values on failure. | ||
return Experiments{}, err | ||
} | ||
|
||
r, err := readJSONInput(f) | ||
|
||
return r, err | ||
} | ||
|
||
// ReadJSONInput takes a reader and unmarshals the contents into the experiments map. | ||
// If the unmarsahlling fails, the experiments map is set to an empty map. | ||
func readJSONInput(b []byte) (Experiments, error) { | ||
var experiments Experiments | ||
if err := json.Unmarshal(b, &experiments); err != nil { | ||
// Return default values on failure. | ||
return Experiments{}, fmt.Errorf("failed to unmarshal json: %w", err) | ||
} | ||
return experiments, 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,52 @@ | ||
package experiments | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestExperiments(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
}{ | ||
{input: "{\"EnableTestFeatureForImage\":true,\"EnableSignedContainerImage\":true}"}, | ||
{input: "{\"EnableTestFeatureForImage\":true,\"EnableSignedContainerImage\":true,\"FloatFeature\":-5.6,\"OtherTestFeatureForImage\":false}"}, | ||
} | ||
|
||
for i, test := range tests { | ||
e, err := readJSONInput([]byte(test.input)) | ||
|
||
if err != nil { | ||
t.Errorf("testcase %d: failed to create experiments object: %v", i, err) | ||
} | ||
|
||
if e.EnableTestFeatureForImage == false { | ||
t.Errorf("testcase %d: expected EnableTestFeatureForImage to be true, got false", i) | ||
} | ||
|
||
if e.EnableSignedContainerImage == false { | ||
t.Errorf("testcase %d: expected EnableSignedContainerImage to be true, got false", i) | ||
} | ||
} | ||
} | ||
|
||
func TestExperimentsBadJson(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
}{ | ||
{input: "{\"EnableTestFeatureForImage\":true,\"EnableSignedContainerImage\":true"}, | ||
{input: "{}"}, | ||
{input: ""}, | ||
} | ||
|
||
for i, test := range tests { | ||
e, _ := readJSONInput([]byte(test.input)) | ||
|
||
if e.EnableTestFeatureForImage == true { | ||
t.Errorf("testcase %d: expected EnableTestFeatureForImage to be false, got true", i) | ||
} | ||
|
||
if e.EnableSignedContainerImage == true { | ||
t.Errorf("testcase %d: expected EnableSignedContainerImage to be false, got true", i) | ||
} | ||
} | ||
} |
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