-
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.
fix: fix broken write to docker config.json (#273)
- Loading branch information
Showing
7 changed files
with
87 additions
and
13 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
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,56 @@ | ||
package plugin | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestWriteDockerConf(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
conf string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "valid json config", | ||
conf: `{"auths":{"registry.example.com":{"auth":"dXNlcjpwYXNz"}}}`, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "invalid json config", | ||
conf: `{"auths":invalid}`, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tmpFile := filepath.Join(t.TempDir(), "config.json") | ||
|
||
err := WriteDockerConf(tmpFile, tt.conf) | ||
if tt.wantErr { | ||
assert.ErrorAs(t, err, &errInvalidDockerConfig) | ||
|
||
return | ||
} | ||
|
||
assert.NoError(t, err) | ||
|
||
content, err := os.ReadFile(tmpFile) | ||
assert.NoError(t, err, "Failed to read config file") | ||
|
||
var got, want interface{} | ||
err = json.Unmarshal(content, &got) | ||
assert.NoError(t, err, "Failed to parse written config") | ||
|
||
err = json.Unmarshal([]byte(tt.conf), &want) | ||
assert.NoError(t, err, "Failed to parse test config") | ||
|
||
assert.Equal(t, want, got, "Written config does not match expected") | ||
}) | ||
} | ||
} |