Skip to content

Commit

Permalink
Merge branch 'main' into dev-middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
gruyaume authored Sep 20, 2024
2 parents 355b628 + 651d7fc commit 55a6717
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 34 deletions.
31 changes: 17 additions & 14 deletions cmd/notary/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,29 +104,32 @@ pebble_notices: false`
func TestMain(m *testing.M) {
cmd := exec.Command("go", "install", "./...")
if err := cmd.Run(); err != nil {
log.Fatalf("couldn't install the notary CLI")
log.Fatalf("couldn't install the notary CLI: %v", err)
}

testfolder, err := os.MkdirTemp("./", "configtest-")
if err != nil {
log.Fatalf("couldn't create temp directory")
log.Fatalf("couldn't create temp directory: %v", err)
}
writeCertErr := os.WriteFile(testfolder+"/cert_test.pem", []byte(validCert), 0o644)
writeKeyErr := os.WriteFile(testfolder+"/key_test.pem", []byte(validPK), 0o644)
if writeCertErr != nil || writeKeyErr != nil {
log.Fatalf("couldn't create temp testing file")
err = os.WriteFile(testfolder+"/cert_test.pem", []byte(validCert), 0o644)
if err != nil {
log.Fatalf("couldn't create temp testing file: %v", err)
}
err = os.WriteFile(testfolder+"/key_test.pem", []byte(validPK), 0o644)
if err != nil {
log.Fatalf("couldn't create temp testing file: %v", err)
}
if err := os.Chdir(testfolder); err != nil {
log.Fatalf("couldn't enter testing directory")
log.Fatalf("couldn't enter testing directory: %v", err)
}

exitval := m.Run()

if err := os.Chdir("../"); err != nil {
log.Fatalf("couldn't change back to parent directory")
log.Fatalf("couldn't change back to parent directory: %v", err)
}
if err := os.RemoveAll(testfolder); err != nil {
log.Fatalf("couldn't remove temp testing directory")
log.Fatalf("couldn't remove temp testing directory: %v", err)
}
os.Exit(exitval)
}
Expand All @@ -145,22 +148,22 @@ func TestNotaryFail(t *testing.T) {
{"database not connectable", []string{"-config", "config.yaml"}, invalidDBConfig, "Couldn't connect to database:"},
}
for _, tc := range cases {
writeConfigErr := os.WriteFile("config.yaml", []byte(tc.ConfigYAML), 0o644)
if writeConfigErr != nil {
t.Errorf("Failed writing config file")
err := os.WriteFile("config.yaml", []byte(tc.ConfigYAML), 0o644)
if err != nil {
t.Errorf("Failed writing config file: %v", err)
}
flag.CommandLine = flag.NewFlagSet(tc.Name, flag.ExitOnError)
cmd := exec.Command("notary", tc.Args...)
stdout, _ := cmd.StdoutPipe()

if err := cmd.Start(); err != nil {
t.Errorf("Failed running command")
t.Errorf("Failed running command: %v", err)
}

slurp, _ := io.ReadAll(stdout)

if err := cmd.Wait(); err == nil {
t.Errorf("Command did not fail")
t.Errorf("Command did not fail: %s", tc.Name)
}
if !strings.Contains(string(slurp), tc.ExpectedOutput) {
t.Errorf("%s: Expected error not found: %s", tc.Name, slurp)
Expand Down
27 changes: 15 additions & 12 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,27 @@ yaml.here`
func TestMain(m *testing.M) {
testfolder, err := os.MkdirTemp("./", "configtest-")
if err != nil {
log.Fatalf("couldn't create temp directory")
log.Fatalf("couldn't create temp directory: %s", err)
}
writeCertErr := os.WriteFile(testfolder+"/cert_test.pem", []byte(validCert), 0o644)
writeKeyErr := os.WriteFile(testfolder+"/key_test.pem", []byte(validPK), 0o644)
if writeCertErr != nil || writeKeyErr != nil {
log.Fatalf("couldn't create temp testing file")
err = os.WriteFile(testfolder+"/cert_test.pem", []byte(validCert), 0o644)
if err != nil {
log.Fatalf("couldn't create temp testing file: %s", err)
}
err = os.WriteFile(testfolder+"/key_test.pem", []byte(validPK), 0o644)
if err != nil {
log.Fatalf("couldn't create temp testing file: %s", err)
}
if err := os.Chdir(testfolder); err != nil {
log.Fatalf("couldn't enter testing directory")
log.Fatalf("couldn't enter testing directory: %s", err)
}

exitval := m.Run()

if err := os.Chdir("../"); err != nil {
log.Fatalf("couldn't change back to parent directory")
log.Fatalf("couldn't change back to parent directory: %s", err)
}
if err := os.RemoveAll(testfolder); err != nil {
log.Fatalf("couldn't remove temp testing directory")
log.Fatalf("couldn't remove temp testing directory: %s", err)
}
os.Exit(exitval)
}
Expand Down Expand Up @@ -104,11 +107,11 @@ func TestBadConfigFail(t *testing.T) {
}

for _, tc := range cases {
writeConfigErr := os.WriteFile("config.yaml", []byte(tc.ConfigYAML), 0o644)
if writeConfigErr != nil {
t.Errorf("Failed writing config file")
err := os.WriteFile("config.yaml", []byte(tc.ConfigYAML), 0o644)
if err != nil {
t.Errorf("Failed writing config file: %v", err)
}
_, err := config.Validate("config.yaml")
_, err = config.Validate("config.yaml")
if err == nil {
t.Errorf("Expected error, got nil")
}
Expand Down
19 changes: 11 additions & 8 deletions internal/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,27 @@ D7DC34n8CH9+avz9sCRwxpjxKnYW/BeyK0c4n9uZpjI8N4sOVqy6yWBUseww
func TestMain(m *testing.M) {
testfolder, err := os.MkdirTemp("./", "configtest-")
if err != nil {
log.Fatalf("couldn't create temp directory")
log.Fatalf("couldn't create temp directory: %s", err)
}
writeCertErr := os.WriteFile(testfolder+"/cert_test.pem", []byte(validCert), 0o644)
writeKeyErr := os.WriteFile(testfolder+"/key_test.pem", []byte(validPK), 0o644)
if writeCertErr != nil || writeKeyErr != nil {
log.Fatalf("couldn't create temp testing file")
err = os.WriteFile(testfolder+"/cert_test.pem", []byte(validCert), 0o644)
if err != nil {
log.Fatalf("couldn't create temp testing file: %s", err)
}
err = os.WriteFile(testfolder+"/key_test.pem", []byte(validPK), 0o644)
if err != nil {
log.Fatalf("couldn't create temp testing file: %s", err)
}
if err := os.Chdir(testfolder); err != nil {
log.Fatalf("couldn't enter testing directory")
log.Fatalf("couldn't enter testing directory: %s", err)
}

exitval := m.Run()

if err := os.Chdir("../"); err != nil {
log.Fatalf("couldn't change back to parent directory")
log.Fatalf("couldn't change back to parent directory: %s", err)
}
if err := os.RemoveAll(testfolder); err != nil {
log.Fatalf("couldn't remove temp testing directory")
log.Fatalf("couldn't remove temp testing directory: %s", err)
}
os.Exit(exitval)
}
Expand Down

0 comments on commit 55a6717

Please sign in to comment.