-
Notifications
You must be signed in to change notification settings - Fork 0
Major refactoring to make slsm simpler and easier to work with #9
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "gopkg.in/yaml.v3" | ||
| ) | ||
|
|
||
| // config is a subset of Sunlight's Config. We use it to load config information | ||
| // about each log. | ||
| type config struct { | ||
| Logs []logConfig | ||
| } | ||
|
|
||
| // logConfig is a subset of Sunlight's LogConfig. We use it to load just the | ||
| // info we need about each individual log. | ||
| type logConfig struct { | ||
| // Name is the unique human-readable identifier of the log. We use it for | ||
| // logging purposes. | ||
| Name string | ||
| // Secret is the path to the file where the sunlight instance expects to | ||
| // find this log's secret seed. We write the seed to this path. | ||
| Secret string | ||
| } | ||
|
|
||
| // loadConfig takes path to a yaml file and returns the seeds in that log file. | ||
| // Exported for use in main.go. | ||
| func loadConfig(configFile string) (*config, error) { | ||
| yml, err := os.ReadFile(configFile) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("reading config file %q: %w", configFile, err) | ||
| } | ||
|
|
||
| var sunlightConfig config | ||
| err = yaml.Unmarshal(yml, &sunlightConfig) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("parsing config file %q: %w", configFile, err) | ||
| } | ||
|
|
||
| if len(sunlightConfig.Logs) == 0 { | ||
| return nil, fmt.Errorf("no logs found in config file %q", configFile) | ||
| } | ||
|
|
||
| for _, log := range sunlightConfig.Logs { | ||
| if log.Name == "" || log.Secret == "" { | ||
| return nil, fmt.Errorf("incomplete config for log %q in config file %q", log.Name, configFile) | ||
| } | ||
| } | ||
|
|
||
| return &sunlightConfig, nil | ||
| } |
This file contains hidden or 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,87 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "path/filepath" | ||
| "reflect" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestLoadConfig(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| for _, tc := range []struct { | ||
| name string | ||
| input string | ||
| want *config | ||
| wantErr string | ||
| }{ | ||
| { | ||
| name: "invalid path", | ||
| input: "", | ||
| want: nil, | ||
| wantErr: "reading config file", | ||
| }, | ||
| { | ||
| name: "invalid yaml", | ||
| input: "invalid.yaml", | ||
| want: nil, | ||
| wantErr: "parsing config file", | ||
| }, | ||
| { | ||
| name: "empty", | ||
| input: "empty.yaml", | ||
| want: nil, | ||
| wantErr: "no logs found in config file", // an empty file is valid yaml | ||
| }, | ||
| { | ||
| name: "no logs", | ||
| input: "nologs.yaml", | ||
| want: nil, | ||
| wantErr: "no logs found in config file", | ||
| }, | ||
| { | ||
| name: "incomplete", | ||
| input: "missingkeys.yaml", | ||
| want: nil, | ||
| wantErr: "incomplete config for log", | ||
| }, | ||
| { | ||
| name: "happy path", | ||
| input: "happy.yaml", | ||
| want: &config{ | ||
| Logs: []logConfig{ | ||
| { | ||
| Name: "test.tld/shard1", | ||
| Secret: "/path/to/shard1.seed", | ||
| }, | ||
| { | ||
| Name: "test.tld/shard2", | ||
| Secret: "/path/to/shard2.seed", | ||
| }, | ||
| }, | ||
| }, | ||
| wantErr: "", | ||
| }, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| got, err := loadConfig(filepath.Join("testdata", tc.input)) | ||
|
|
||
| if tc.wantErr != "" { //nolint:nestif | ||
| if err == nil { | ||
| t.Errorf("loadConfig() = %#v, but want %q", got, tc.wantErr) | ||
| } else if !strings.Contains(err.Error(), tc.wantErr) { | ||
| t.Errorf("loadConfig() = %q, but want %q", err, tc.wantErr) | ||
| } | ||
| } else { | ||
| if err != nil { | ||
| t.Errorf("loadConfig() = %q, but want %#v", err, tc.want) | ||
| } else if !reflect.DeepEqual(got, tc.want) { | ||
| t.Errorf("loadConfig() = %#v, but want %#v", got, tc.want) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or 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,47 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "syscall" | ||
| ) | ||
|
|
||
| func writeFile(path string, content []byte, fsType int64) error { | ||
| // We currently assume that the directory we are trying to write to already exists. | ||
| file, err := os.OpenFile( | ||
| path, | ||
| // The combination of O_CREATE and O_EXCL means this operation will fail | ||
| // if the file already exists. | ||
| os.O_RDWR|os.O_CREATE|os.O_EXCL, | ||
| // Setting nolint here because file permissions octal value isn't a magic number. | ||
| //nolint: mnd | ||
| 0o400, | ||
| ) | ||
| if err != nil { | ||
| return fmt.Errorf("creating file at path %q: %w", path, err) | ||
| } | ||
| defer file.Close() | ||
|
|
||
| var statfs syscall.Statfs_t | ||
| err = syscall.Fstatfs(int(file.Fd()), &statfs) | ||
| if err != nil { | ||
| _ = os.Remove(file.Name()) | ||
|
|
||
| return fmt.Errorf("getting filesystem info at path %q: %w", path, err) | ||
| } | ||
|
|
||
| if statfs.Type != fsType { | ||
| _ = os.Remove(file.Name()) | ||
|
|
||
| return fmt.Errorf("filesystem at path %q has type %v, but we require %v", path, statfs.Type, fsType) | ||
| } | ||
|
|
||
| _, err = file.Write(content) | ||
| if err != nil { | ||
| _ = os.Remove(file.Name()) | ||
|
|
||
| return fmt.Errorf("writing to file at path %q: %w", path, err) | ||
| } | ||
|
|
||
| return nil | ||
| } |
This file contains hidden or 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,76 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestWriteFile(t *testing.T) { | ||
| t.Parallel() | ||
| tempDir := t.TempDir() | ||
|
|
||
| // Do some setup for the "already exists" test. | ||
| f, err := os.Create(filepath.Join(tempDir, "already-exists")) | ||
| if err != nil { | ||
| t.Fatalf("failed to create test setup file: %s", err) | ||
| } | ||
| err = f.Close() | ||
| if err != nil { | ||
| t.Fatalf("failed to close test setup file: %s", err) | ||
| } | ||
|
|
||
| for _, tc := range []struct { | ||
| name string | ||
| path string | ||
| fsType int64 | ||
| wantErr string | ||
| }{ | ||
| { | ||
| name: "already exists", | ||
| path: filepath.Join(tempDir, "already-exists"), | ||
| fsType: 1, | ||
| wantErr: "creating file at path", | ||
| }, | ||
| { | ||
| name: "wrong fstype", | ||
| path: filepath.Join(tempDir, "wrong-fstype"), | ||
| fsType: 1, | ||
| wantErr: "filesystem at path", | ||
| }, | ||
| { | ||
| name: "happy path", | ||
| path: filepath.Join(tempDir, "happy"), | ||
| fsType: 61267, // The statfs.Type for a normal unix filesystem | ||
| wantErr: "", | ||
| }, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| err := writeFile(tc.path, []byte("hello world"), tc.fsType) | ||
|
|
||
| if tc.wantErr != "" { //nolint:nestif | ||
| if err == nil { | ||
| t.Errorf("writeFile() = succeeded, but want error %q", tc.wantErr) | ||
| } else if !strings.Contains(err.Error(), tc.wantErr) { | ||
| t.Errorf("writeFile() = %#v, but want %q", err, tc.wantErr) | ||
| } | ||
| } else { | ||
| if err != nil { | ||
| t.Fatalf("writeFile() = %#v, but want success", err) | ||
| } | ||
|
|
||
| got, err := os.ReadFile(tc.path) | ||
| if err != nil { | ||
| t.Fatalf("failed to re-read file: %s", err) | ||
| } | ||
|
|
||
| if string(got) != "hello world" { | ||
| t.Errorf("written file contains %q, but want %q", string(got), "hello world") | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know you're not touching this line, but I'm pretty sure this WithSharedConfigProfile line isn't needed