2022-10-10 15:05:55 +03:00
//go:build search
// +build search
2020-10-14 14:47:20 -07:00
2023-09-16 01:17:01 +03:00
package client
2020-06-16 21:52:40 -04:00
import (
"bytes"
2022-06-02 17:14:21 +03:00
"log"
2020-06-16 21:52:40 -04:00
"os"
2023-07-13 19:34:01 +03:00
"regexp"
2020-06-16 21:52:40 -04:00
"strings"
"testing"
. "github.com/smartystreets/goconvey/convey"
2022-10-20 19:39:20 +03:00
2023-08-30 20:12:24 +03:00
zerr "zotregistry.io/zot/errors"
2020-06-16 21:52:40 -04:00
)
func TestConfigCmdBasics ( t * testing . T ) {
Convey ( "Test config help" , t , func ( ) {
args := [ ] string { "--help" }
configPath := makeConfigFile ( "showspinner = false" )
defer os . Remove ( configPath )
2020-07-14 13:11:01 -04:00
cmd := NewConfigCommand ( )
2020-06-16 21:52:40 -04:00
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
2021-05-21 20:47:28 +00:00
cmd . SetErr ( buff )
2020-06-16 21:52:40 -04:00
cmd . SetArgs ( args )
err := cmd . Execute ( )
So ( buff . String ( ) , ShouldContainSubstring , "Usage" )
So ( err , ShouldBeNil )
Convey ( "with the shorthand" , func ( ) {
args [ 0 ] = "-h"
configPath := makeConfigFile ( "showspinner = false" )
defer os . Remove ( configPath )
2020-07-14 13:11:01 -04:00
cmd := NewConfigCommand ( )
2020-06-16 21:52:40 -04:00
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
2021-05-21 20:47:28 +00:00
cmd . SetErr ( buff )
2020-06-16 21:52:40 -04:00
cmd . SetArgs ( args )
err := cmd . Execute ( )
So ( buff . String ( ) , ShouldContainSubstring , "Usage" )
So ( err , ShouldBeNil )
} )
} )
Convey ( "Test config no args" , t , func ( ) {
args := [ ] string { }
configPath := makeConfigFile ( "showspinner = false" )
defer os . Remove ( configPath )
2020-07-14 13:11:01 -04:00
cmd := NewConfigCommand ( )
2020-06-16 21:52:40 -04:00
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
2021-05-21 20:47:28 +00:00
cmd . SetErr ( buff )
2020-06-16 21:52:40 -04:00
cmd . SetArgs ( args )
err := cmd . Execute ( )
So ( buff . String ( ) , ShouldContainSubstring , "Usage" )
So ( err , ShouldNotBeNil )
} )
}
func TestConfigCmdMain ( t * testing . T ) {
Convey ( "Test add config" , t , func ( ) {
args := [ ] string { "add" , "configtest1" , "https://test-url.com" }
file := makeConfigFile ( "" )
defer os . Remove ( file )
2020-07-14 13:11:01 -04:00
cmd := NewConfigCommand ( )
2020-06-16 21:52:40 -04:00
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
2021-05-21 20:47:28 +00:00
cmd . SetErr ( buff )
2020-06-16 21:52:40 -04:00
cmd . SetArgs ( args )
_ = cmd . Execute ( )
2022-09-02 14:56:02 +02:00
actual , err := os . ReadFile ( file )
2020-06-16 21:52:40 -04:00
if err != nil {
panic ( err )
}
actualStr := string ( actual )
So ( actualStr , ShouldContainSubstring , "configtest1" )
So ( actualStr , ShouldContainSubstring , "https://test-url.com" )
} )
2022-06-02 17:14:21 +03:00
Convey ( "Test error on home directory" , t , func ( ) {
args := [ ] string { "add" , "configtest1" , "https://test-url.com" }
file := makeConfigFile ( "" )
defer os . Remove ( file )
err := os . Setenv ( "HOME" , "nonExistentDirectory" )
if err != nil {
panic ( err )
}
cmd := NewConfigCommand ( )
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
cmd . SetErr ( buff )
cmd . SetArgs ( args )
err = cmd . Execute ( )
So ( err , ShouldNotBeNil )
home , err := os . UserHomeDir ( )
if err != nil {
panic ( err )
}
err = os . Setenv ( "HOME" , home )
if err != nil {
log . Fatal ( err )
}
} )
Convey ( "Test error on home directory at new add config" , t , func ( ) {
args := [ ] string { "add" , "configtest1" , "https://test-url.com" }
file := makeConfigFile ( "" )
defer os . Remove ( file )
err := os . Setenv ( "HOME" , "nonExistentDirectory" )
if err != nil {
panic ( err )
}
cmd := NewConfigAddCommand ( )
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
cmd . SetErr ( buff )
cmd . SetArgs ( args )
err = cmd . Execute ( )
So ( err , ShouldNotBeNil )
home , err := os . UserHomeDir ( )
if err != nil {
panic ( err )
}
err = os . Setenv ( "HOME" , home )
if err != nil {
log . Fatal ( err )
}
} )
2022-03-21 17:37:23 +00:00
Convey ( "Test add config with invalid format" , t , func ( ) {
args := [ ] string { "--list" }
configPath := makeConfigFile ( ` { "configs": { "_name":"configtest","url":"https://test-url.com","showspinner":false}} ` )
defer os . Remove ( configPath )
cmd := NewConfigCommand ( )
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
cmd . SetErr ( buff )
cmd . SetArgs ( args )
err := cmd . Execute ( )
2023-08-30 20:12:24 +03:00
So ( err , ShouldEqual , zerr . ErrCliBadConfig )
2022-03-21 17:37:23 +00:00
} )
2020-06-16 21:52:40 -04:00
Convey ( "Test add config with invalid URL" , t , func ( ) {
args := [ ] string { "add" , "configtest1" , "test..com" }
file := makeConfigFile ( "" )
defer os . Remove ( file )
2020-07-14 13:11:01 -04:00
cmd := NewConfigCommand ( )
2020-06-16 21:52:40 -04:00
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
2021-05-21 20:47:28 +00:00
cmd . SetErr ( buff )
2020-06-16 21:52:40 -04:00
cmd . SetArgs ( args )
err := cmd . Execute ( )
So ( err , ShouldNotBeNil )
2023-09-14 20:51:17 +03:00
So ( strings . Contains ( err . Error ( ) , zerr . ErrInvalidURL . Error ( ) ) , ShouldBeTrue )
2020-06-16 21:52:40 -04:00
} )
2023-07-13 19:34:01 +03:00
Convey ( "Test remove config entry successfully" , t , func ( ) {
args := [ ] string { "remove" , "configtest" }
configPath := makeConfigFile ( ` { "configs":[ { "_name":"configtest","url":"https://test-url.com","showspinner":false}]} ` )
defer os . Remove ( configPath )
cmd := NewConfigCommand ( )
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
cmd . SetErr ( buff )
cmd . SetArgs ( args )
err := cmd . Execute ( )
So ( err , ShouldBeNil )
actual , err := os . ReadFile ( configPath )
So ( err , ShouldBeNil )
space := regexp . MustCompile ( ` \s+ ` )
actualString := space . ReplaceAllString ( string ( actual ) , " " )
So ( actualString , ShouldEqual , ` { "configs": [] } ` )
} )
Convey ( "Test remove missing config entry" , t , func ( ) {
args := [ ] string { "remove" , "configtest" }
configPath := makeConfigFile ( ` { "configs":[] ` )
defer os . Remove ( configPath )
cmd := NewConfigCommand ( )
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
cmd . SetErr ( buff )
cmd . SetArgs ( args )
err := cmd . Execute ( )
So ( err , ShouldNotBeNil )
So ( buff . String ( ) , ShouldContainSubstring , "does not exist" )
} )
Convey ( "Test remove bad config file content" , t , func ( ) {
args := [ ] string { "remove" , "configtest" }
configPath := makeConfigFile ( ` { "asdf":[] ` )
defer os . Remove ( configPath )
cmd := NewConfigCommand ( )
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
cmd . SetErr ( buff )
cmd . SetArgs ( args )
err := cmd . Execute ( )
So ( err , ShouldNotBeNil )
So ( buff . String ( ) , ShouldContainSubstring , "config json is empty" )
} )
Convey ( "Test remove bad config file entry" , t , func ( ) {
args := [ ] string { "remove" , "configtest" }
configPath := makeConfigFile ( ` { "configs":[asdad] ` )
defer os . Remove ( configPath )
cmd := NewConfigCommand ( )
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
cmd . SetErr ( buff )
cmd . SetArgs ( args )
err := cmd . Execute ( )
So ( err , ShouldNotBeNil )
So ( buff . String ( ) , ShouldContainSubstring , "invalid config" )
} )
Convey ( "Test remove config bad permissions" , t , func ( ) {
args := [ ] string { "remove" , "configtest" }
configPath := makeConfigFile ( ` { "configs":[ { "_name":"configtest","url":"https://test-url.com","showspinner":false}]} ` )
defer func ( ) {
_ = os . Chmod ( configPath , 0 o600 )
os . Remove ( configPath )
} ( )
err := os . Chmod ( configPath , 0 o400 ) // Read-only, so we fail only on updating the file, not reading
So ( err , ShouldBeNil )
cmd := NewConfigCommand ( )
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
cmd . SetErr ( buff )
cmd . SetArgs ( args )
err = cmd . Execute ( )
So ( err , ShouldNotBeNil )
So ( buff . String ( ) , ShouldContainSubstring , "permission denied" )
} )
2020-06-16 21:52:40 -04:00
Convey ( "Test fetch all config" , t , func ( ) {
args := [ ] string { "--list" }
configPath := makeConfigFile ( ` { "configs":[ { "_name":"configtest","url":"https://test-url.com","showspinner":false}]} ` )
defer os . Remove ( configPath )
2020-07-14 13:11:01 -04:00
cmd := NewConfigCommand ( )
2020-06-16 21:52:40 -04:00
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
2021-05-21 20:47:28 +00:00
cmd . SetErr ( buff )
2020-06-16 21:52:40 -04:00
cmd . SetArgs ( args )
err := cmd . Execute ( )
So ( buff . String ( ) , ShouldContainSubstring , "https://test-url.com" )
So ( err , ShouldBeNil )
Convey ( "with the shorthand" , func ( ) {
args := [ ] string { "-l" }
configPath := makeConfigFile ( ` { "configs":[ { "_name":"configtest","url":"https://test-url.com","showspinner":false}]} ` )
defer os . Remove ( configPath )
2020-07-14 13:11:01 -04:00
cmd := NewConfigCommand ( )
2020-06-16 21:52:40 -04:00
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
2021-05-21 20:47:28 +00:00
cmd . SetErr ( buff )
2020-06-16 21:52:40 -04:00
cmd . SetArgs ( args )
err := cmd . Execute ( )
So ( buff . String ( ) , ShouldContainSubstring , "https://test-url.com" )
So ( err , ShouldBeNil )
} )
Convey ( "From empty file" , func ( ) {
args := [ ] string { "-l" }
configPath := makeConfigFile ( ` ` )
defer os . Remove ( configPath )
2020-07-14 13:11:01 -04:00
cmd := NewConfigCommand ( )
2020-06-16 21:52:40 -04:00
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
2021-05-21 20:47:28 +00:00
cmd . SetErr ( buff )
2020-06-16 21:52:40 -04:00
cmd . SetArgs ( args )
err := cmd . Execute ( )
So ( err , ShouldBeNil )
So ( strings . TrimSpace ( buff . String ( ) ) , ShouldEqual , "" )
} )
} )
Convey ( "Test fetch a config" , t , func ( ) {
args := [ ] string { "configtest" , "--list" }
configPath := makeConfigFile ( ` { "configs":[ { "_name":"configtest","url":"https://test-url.com","showspinner":false}]} ` )
defer os . Remove ( configPath )
2020-07-14 13:11:01 -04:00
cmd := NewConfigCommand ( )
2020-06-16 21:52:40 -04:00
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
2021-05-21 20:47:28 +00:00
cmd . SetErr ( buff )
2020-06-16 21:52:40 -04:00
cmd . SetArgs ( args )
err := cmd . Execute ( )
So ( buff . String ( ) , ShouldContainSubstring , "url = https://test-url.com" )
So ( buff . String ( ) , ShouldContainSubstring , "showspinner = false" )
So ( err , ShouldBeNil )
Convey ( "with the shorthand" , func ( ) {
args := [ ] string { "configtest" , "-l" }
configPath := makeConfigFile ( ` { "configs":[ { "_name":"configtest","url":"https://test-url.com","showspinner":false}]} ` )
defer os . Remove ( configPath )
2020-07-14 13:11:01 -04:00
cmd := NewConfigCommand ( )
2020-06-16 21:52:40 -04:00
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
2021-05-21 20:47:28 +00:00
cmd . SetErr ( buff )
2020-06-16 21:52:40 -04:00
cmd . SetArgs ( args )
err := cmd . Execute ( )
So ( buff . String ( ) , ShouldContainSubstring , "url = https://test-url.com" )
So ( buff . String ( ) , ShouldContainSubstring , "showspinner = false" )
So ( err , ShouldBeNil )
} )
Convey ( "From empty file" , func ( ) {
args := [ ] string { "configtest" , "-l" }
configPath := makeConfigFile ( ` ` )
defer os . Remove ( configPath )
2020-07-14 13:11:01 -04:00
cmd := NewConfigCommand ( )
2020-06-16 21:52:40 -04:00
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
2021-05-21 20:47:28 +00:00
cmd . SetErr ( buff )
2020-06-16 21:52:40 -04:00
cmd . SetArgs ( args )
err := cmd . Execute ( )
So ( err , ShouldBeNil )
So ( strings . TrimSpace ( buff . String ( ) ) , ShouldEqual , "" )
} )
} )
Convey ( "Test fetch a config val" , t , func ( ) {
args := [ ] string { "configtest" , "url" }
configPath := makeConfigFile ( ` { "configs":[ { "_name":"configtest","url":"https://test-url.com","showspinner":false}]} ` )
defer os . Remove ( configPath )
2020-07-14 13:11:01 -04:00
cmd := NewConfigCommand ( )
2020-06-16 21:52:40 -04:00
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
2021-05-21 20:47:28 +00:00
cmd . SetErr ( buff )
2020-06-16 21:52:40 -04:00
cmd . SetArgs ( args )
err := cmd . Execute ( )
So ( buff . String ( ) , ShouldEqual , "https://test-url.com\n" )
So ( err , ShouldBeNil )
Convey ( "From empty file" , func ( ) {
args := [ ] string { "configtest" , "url" }
configPath := makeConfigFile ( ` ` )
defer os . Remove ( configPath )
2020-07-14 13:11:01 -04:00
cmd := NewConfigCommand ( )
2020-06-16 21:52:40 -04:00
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
cmd . SetErr ( buff )
cmd . SetArgs ( args )
err := cmd . Execute ( )
So ( err , ShouldNotBeNil )
So ( buff . String ( ) , ShouldContainSubstring , "does not exist" )
} )
} )
Convey ( "Test add a config val" , t , func ( ) {
args := [ ] string { "configtest" , "showspinner" , "false" }
configPath := makeConfigFile ( ` { "configs":[ { "_name":"configtest","url":"https://test-url.com"}]} ` )
defer os . Remove ( configPath )
2020-07-14 13:11:01 -04:00
cmd := NewConfigCommand ( )
2020-06-16 21:52:40 -04:00
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
2021-05-21 20:47:28 +00:00
cmd . SetErr ( buff )
2020-06-16 21:52:40 -04:00
cmd . SetArgs ( args )
err := cmd . Execute ( )
So ( err , ShouldBeNil )
2022-09-02 14:56:02 +02:00
actual , err := os . ReadFile ( configPath )
2020-06-16 21:52:40 -04:00
if err != nil {
panic ( err )
}
actualStr := string ( actual )
So ( actualStr , ShouldContainSubstring , "https://test-url.com" )
2023-07-13 19:34:01 +03:00
So ( actualStr , ShouldContainSubstring , ` "showspinner": false ` )
2020-06-16 21:52:40 -04:00
So ( buff . String ( ) , ShouldEqual , "" )
Convey ( "To an empty file" , func ( ) {
args := [ ] string { "configtest" , "showspinner" , "false" }
configPath := makeConfigFile ( ` ` )
defer os . Remove ( configPath )
2020-07-14 13:11:01 -04:00
cmd := NewConfigCommand ( )
2020-06-16 21:52:40 -04:00
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
2021-05-21 20:47:28 +00:00
cmd . SetErr ( buff )
2020-06-16 21:52:40 -04:00
cmd . SetArgs ( args )
err := cmd . Execute ( )
So ( err , ShouldNotBeNil )
So ( buff . String ( ) , ShouldContainSubstring , "does not exist" )
} )
} )
Convey ( "Test overwrite a config" , t , func ( ) {
args := [ ] string { "configtest" , "url" , "https://new-url.com" }
configPath := makeConfigFile ( ` { "configs":[ { "_name":"configtest","url":"https://test-url.com","showspinner":false}]} ` )
defer os . Remove ( configPath )
2020-07-14 13:11:01 -04:00
cmd := NewConfigCommand ( )
2020-06-16 21:52:40 -04:00
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
2021-05-21 20:47:28 +00:00
cmd . SetErr ( buff )
2020-06-16 21:52:40 -04:00
cmd . SetArgs ( args )
err := cmd . Execute ( )
So ( err , ShouldBeNil )
2022-09-02 14:56:02 +02:00
actual , err := os . ReadFile ( configPath )
2020-06-16 21:52:40 -04:00
if err != nil {
panic ( err )
}
actualStr := string ( actual )
So ( actualStr , ShouldContainSubstring , ` https://new-url.com ` )
2023-07-13 19:34:01 +03:00
So ( actualStr , ShouldContainSubstring , ` "showspinner": false ` )
2020-06-16 21:52:40 -04:00
So ( actualStr , ShouldNotContainSubstring , ` https://test-url.com ` )
So ( buff . String ( ) , ShouldEqual , "" )
} )
Convey ( "Test reset a config val" , t , func ( ) {
args := [ ] string { "configtest" , "showspinner" , "--reset" }
configPath := makeConfigFile ( ` { "configs":[ { "_name":"configtest","url":"https://test-url.com","showspinner":false}]} ` )
defer os . Remove ( configPath )
2020-07-14 13:11:01 -04:00
cmd := NewConfigCommand ( )
2020-06-16 21:52:40 -04:00
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
2021-05-21 20:47:28 +00:00
cmd . SetErr ( buff )
2020-06-16 21:52:40 -04:00
cmd . SetArgs ( args )
err := cmd . Execute ( )
So ( err , ShouldBeNil )
2022-09-02 14:56:02 +02:00
actual , err := os . ReadFile ( configPath )
2020-06-16 21:52:40 -04:00
if err != nil {
panic ( err )
}
actualStr := string ( actual )
So ( actualStr , ShouldNotContainSubstring , "showspinner" )
2023-07-13 19:34:01 +03:00
So ( actualStr , ShouldContainSubstring , ` "url": "https://test-url.com" ` )
2020-06-16 21:52:40 -04:00
So ( buff . String ( ) , ShouldEqual , "" )
} )
Convey ( "Test reset a url" , t , func ( ) {
args := [ ] string { "configtest" , "url" , "--reset" }
configPath := makeConfigFile ( ` { "configs":[ { "_name":"configtest","url":"https://test-url.com","showspinner":false}]} ` )
defer os . Remove ( configPath )
2020-07-14 13:11:01 -04:00
cmd := NewConfigCommand ( )
2020-06-16 21:52:40 -04:00
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
2021-05-21 20:47:28 +00:00
cmd . SetErr ( buff )
2020-06-16 21:52:40 -04:00
cmd . SetArgs ( args )
err := cmd . Execute ( )
So ( err , ShouldNotBeNil )
So ( buff . String ( ) , ShouldContainSubstring , "cannot reset" )
} )
2020-10-29 14:47:27 +02:00
Convey ( "Test add a config with an existing saved name" , t , func ( ) {
args := [ ] string { "add" , "configtest" , "https://test-url.com/new" }
configPath := makeConfigFile ( ` { "configs":[ { "_name":"configtest","url":"https://test-url.com","showspinner":false}]} ` )
defer os . Remove ( configPath )
cmd := NewConfigCommand ( )
buff := bytes . NewBufferString ( "" )
cmd . SetOut ( buff )
2021-05-21 20:47:28 +00:00
cmd . SetErr ( buff )
2020-10-29 14:47:27 +02:00
cmd . SetArgs ( args )
err := cmd . Execute ( )
So ( err , ShouldNotBeNil )
So ( buff . String ( ) , ShouldContainSubstring , "cli config name already added" )
} )
2020-06-16 21:52:40 -04:00
}