2022-10-10 07:05:55 -05:00
//go:build sync && scrub && metrics && search
// +build sync,scrub,metrics,search
2021-12-28 08:29:30 -05:00
package cli_test
import (
2022-09-02 08:40:34 -05:00
"context"
2021-12-28 08:29:30 -05:00
"fmt"
"net/http"
"os"
2022-09-02 08:40:34 -05:00
"strings"
2021-12-28 08:29:30 -05:00
"testing"
2022-09-02 08:40:34 -05:00
"time"
2021-12-28 08:29:30 -05:00
. "github.com/smartystreets/goconvey/convey"
"gopkg.in/resty.v1"
2022-10-20 11:39:20 -05:00
2021-12-28 08:29:30 -05:00
"zotregistry.io/zot/pkg/cli"
. "zotregistry.io/zot/pkg/test"
)
2022-09-02 08:40:34 -05:00
const readLogFileTimeout = 5 * time . Second
2021-12-28 08:29:30 -05:00
func TestServeExtensions ( t * testing . T ) {
oldArgs := os . Args
defer func ( ) { os . Args = oldArgs } ( )
Convey ( "config file with no extensions" , t , func ( c C ) {
port := GetFreePort ( )
baseURL := GetBaseURL ( port )
2022-09-02 07:56:02 -05:00
logFile , err := os . CreateTemp ( "" , "zot-log*.txt" )
2021-12-28 08:29:30 -05:00
So ( err , ShouldBeNil )
defer os . Remove ( logFile . Name ( ) ) // clean up
content := fmt . Sprintf ( ` {
"storage" : {
"rootDirectory" : "/tmp/zot"
} ,
"http" : {
"address" : "127.0.0.1" ,
"port" : "%s"
} ,
"log" : {
"level" : "debug" ,
"output" : "%s"
}
} ` , port , logFile . Name ( ) )
2022-09-02 07:56:02 -05:00
cfgfile , err := os . CreateTemp ( "" , "zot-test*.json" )
2021-12-28 08:29:30 -05:00
So ( err , ShouldBeNil )
defer os . Remove ( cfgfile . Name ( ) ) // clean up
_ , err = cfgfile . Write ( [ ] byte ( content ) )
So ( err , ShouldBeNil )
err = cfgfile . Close ( )
So ( err , ShouldBeNil )
os . Args = [ ] string { "cli_test" , "serve" , cfgfile . Name ( ) }
go func ( ) {
err = cli . NewServerRootCmd ( ) . Execute ( )
So ( err , ShouldBeNil )
} ( )
WaitTillServerReady ( baseURL )
data , err := os . ReadFile ( logFile . Name ( ) )
So ( err , ShouldBeNil )
So ( string ( data ) , ShouldContainSubstring , "\"Extensions\":null" )
} )
Convey ( "config file with empty extensions" , t , func ( c C ) {
port := GetFreePort ( )
baseURL := GetBaseURL ( port )
2022-09-02 07:56:02 -05:00
logFile , err := os . CreateTemp ( "" , "zot-log*.txt" )
2021-12-28 08:29:30 -05:00
So ( err , ShouldBeNil )
defer os . Remove ( logFile . Name ( ) ) // clean up
content := fmt . Sprintf ( ` {
"storage" : {
"rootDirectory" : "/tmp/zot"
} ,
"http" : {
"address" : "127.0.0.1" ,
"port" : "%s"
} ,
"log" : {
"level" : "debug" ,
"output" : "%s"
} ,
"extensions" : {
}
} ` , port , logFile . Name ( ) )
2022-09-02 07:56:02 -05:00
cfgfile , err := os . CreateTemp ( "" , "zot-test*.json" )
2021-12-28 08:29:30 -05:00
So ( err , ShouldBeNil )
defer os . Remove ( cfgfile . Name ( ) ) // clean up
_ , err = cfgfile . Write ( [ ] byte ( content ) )
So ( err , ShouldBeNil )
err = cfgfile . Close ( )
So ( err , ShouldBeNil )
os . Args = [ ] string { "cli_test" , "serve" , cfgfile . Name ( ) }
go func ( ) {
err = cli . NewServerRootCmd ( ) . Execute ( )
So ( err , ShouldBeNil )
} ( )
WaitTillServerReady ( baseURL )
data , err := os . ReadFile ( logFile . Name ( ) )
So ( err , ShouldBeNil )
2022-06-24 08:08:47 -05:00
So ( string ( data ) , ShouldContainSubstring , "\"Extensions\":{\"Search\":null,\"Sync\":null,\"Metrics\":null,\"Scrub\":null,\"Lint\":null" ) //nolint:lll // gofumpt conflicts with lll
2021-12-28 08:29:30 -05:00
} )
}
func testWithMetricsEnabled ( cfgContentFormat string ) {
port := GetFreePort ( )
baseURL := GetBaseURL ( port )
2022-09-02 07:56:02 -05:00
logFile , err := os . CreateTemp ( "" , "zot-log*.txt" )
2021-12-28 08:29:30 -05:00
So ( err , ShouldBeNil )
defer os . Remove ( logFile . Name ( ) ) // clean up
content := fmt . Sprintf ( cfgContentFormat , port , logFile . Name ( ) )
2022-09-02 07:56:02 -05:00
cfgfile , err := os . CreateTemp ( "" , "zot-test*.json" )
2021-12-28 08:29:30 -05:00
So ( err , ShouldBeNil )
defer os . Remove ( cfgfile . Name ( ) ) // clean up
_ , err = cfgfile . Write ( [ ] byte ( content ) )
So ( err , ShouldBeNil )
err = cfgfile . Close ( )
So ( err , ShouldBeNil )
os . Args = [ ] string { "cli_test" , "serve" , cfgfile . Name ( ) }
go func ( ) {
err = cli . NewServerRootCmd ( ) . Execute ( )
So ( err , ShouldBeNil )
} ( )
WaitTillServerReady ( baseURL )
resp , err := resty . R ( ) . Get ( baseURL + "/metrics" )
So ( err , ShouldBeNil )
So ( resp , ShouldNotBeNil )
So ( resp . StatusCode ( ) , ShouldEqual , http . StatusOK )
respStr := string ( resp . Body ( ) )
So ( respStr , ShouldContainSubstring , "zot_info" )
data , err := os . ReadFile ( logFile . Name ( ) )
So ( err , ShouldBeNil )
So ( string ( data ) , ShouldContainSubstring ,
2022-06-24 08:08:47 -05:00
"\"Extensions\":{\"Search\":null,\"Sync\":null,\"Metrics\":{\"Enable\":true,\"Prometheus\":{\"Path\":\"/metrics\"}},\"Scrub\":null,\"Lint\":null}" ) //nolint:lll // gofumpt conflicts with lll
2021-12-28 08:29:30 -05:00
}
func TestServeMetricsExtension ( t * testing . T ) {
oldArgs := os . Args
defer func ( ) { os . Args = oldArgs } ( )
Convey ( "no explicit enable" , t , func ( c C ) {
content := ` {
"storage" : {
"rootDirectory" : "/tmp/zot"
} ,
"http" : {
"address" : "127.0.0.1" ,
"port" : "%s"
} ,
"log" : {
"level" : "debug" ,
"output" : "%s"
} ,
"extensions" : {
"metrics" : {
}
}
} `
testWithMetricsEnabled ( content )
} )
Convey ( "no explicit enable but with prometheus parameter" , t , func ( c C ) {
content := ` {
"storage" : {
"rootDirectory" : "/tmp/zot"
} ,
"http" : {
"address" : "127.0.0.1" ,
"port" : "%s"
} ,
"log" : {
"level" : "debug" ,
"output" : "%s"
} ,
"extensions" : {
"metrics" : {
"prometheus" : {
"path" : "/metrics"
}
}
}
} `
testWithMetricsEnabled ( content )
} )
Convey ( "with explicit enable, but without prometheus parameter" , t , func ( c C ) {
content := ` {
"storage" : {
"rootDirectory" : "/tmp/zot"
} ,
"http" : {
"address" : "127.0.0.1" ,
"port" : "%s"
} ,
"log" : {
"level" : "debug" ,
"output" : "%s"
} ,
"extensions" : {
"metrics" : {
"enable" : true
}
}
} `
testWithMetricsEnabled ( content )
} )
Convey ( "with explicit disable" , t , func ( c C ) {
port := GetFreePort ( )
baseURL := GetBaseURL ( port )
2022-09-02 07:56:02 -05:00
logFile , err := os . CreateTemp ( "" , "zot-log*.txt" )
2021-12-28 08:29:30 -05:00
So ( err , ShouldBeNil )
defer os . Remove ( logFile . Name ( ) ) // clean up
content := fmt . Sprintf ( ` {
"storage" : {
"rootDirectory" : "/tmp/zot"
} ,
"http" : {
"address" : "127.0.0.1" ,
"port" : "%s"
} ,
"log" : {
"level" : "debug" ,
"output" : "%s"
} ,
"extensions" : {
"metrics" : {
"enable" : false
}
}
} ` , port , logFile . Name ( ) )
2022-09-02 07:56:02 -05:00
cfgfile , err := os . CreateTemp ( "" , "zot-test*.json" )
2021-12-28 08:29:30 -05:00
So ( err , ShouldBeNil )
defer os . Remove ( cfgfile . Name ( ) ) // clean up
_ , err = cfgfile . Write ( [ ] byte ( content ) )
So ( err , ShouldBeNil )
err = cfgfile . Close ( )
So ( err , ShouldBeNil )
os . Args = [ ] string { "cli_test" , "serve" , cfgfile . Name ( ) }
go func ( ) {
err = cli . NewServerRootCmd ( ) . Execute ( )
So ( err , ShouldBeNil )
} ( )
WaitTillServerReady ( baseURL )
resp , err := resty . R ( ) . Get ( baseURL + "/metrics" )
So ( err , ShouldBeNil )
So ( resp , ShouldNotBeNil )
So ( resp . StatusCode ( ) , ShouldEqual , http . StatusNotFound )
data , err := os . ReadFile ( logFile . Name ( ) )
So ( err , ShouldBeNil )
So ( string ( data ) , ShouldContainSubstring ,
2022-06-24 08:08:47 -05:00
"\"Extensions\":{\"Search\":null,\"Sync\":null,\"Metrics\":{\"Enable\":false,\"Prometheus\":{\"Path\":\"/metrics\"}},\"Scrub\":null,\"Lint\":null}}" ) //nolint:lll // gofumpt conflicts with lll
2021-12-28 08:29:30 -05:00
} )
}
func TestServeSyncExtension ( t * testing . T ) {
oldArgs := os . Args
defer func ( ) { os . Args = oldArgs } ( )
Convey ( "sync implicitly enabled" , t , func ( c C ) {
2022-06-30 10:43:31 -05:00
content := ` {
2021-12-28 08:29:30 -05:00
"storage" : {
2022-06-30 10:43:31 -05:00
"rootDirectory" : "%s"
2021-12-28 08:29:30 -05:00
} ,
"http" : {
"address" : "127.0.0.1" ,
"port" : "%s"
} ,
"log" : {
"level" : "debug" ,
"output" : "%s"
} ,
"extensions" : {
"sync" : {
"registries" : [ {
"urls" : [ "http://localhost:8080" ] ,
"tlsVerify" : false ,
"onDemand" : true ,
"maxRetries" : 3 ,
"retryDelay" : "15m" ,
"certDir" : "" ,
"content" : [
{
"prefix" : "zot-test" ,
"tags" : {
"regex" : ".*" ,
"semver" : true
}
}
]
} ]
}
}
2022-06-30 10:43:31 -05:00
} `
2021-12-28 08:29:30 -05:00
2022-09-02 08:40:34 -05:00
logPath , err := runCLIWithConfig ( t . TempDir ( ) , content )
So ( err , ShouldBeNil )
data , err := os . ReadFile ( logPath )
2021-12-28 08:29:30 -05:00
So ( err , ShouldBeNil )
2022-09-02 08:40:34 -05:00
defer os . Remove ( logPath ) // clean up
So ( string ( data ) , ShouldContainSubstring ,
2021-12-28 08:29:30 -05:00
"\"Extensions\":{\"Search\":null,\"Sync\":{\"Enable\":true" )
} )
Convey ( "sync explicitly enabled" , t , func ( c C ) {
2022-06-30 10:43:31 -05:00
content := ` {
2021-12-28 08:29:30 -05:00
"storage" : {
2022-06-30 10:43:31 -05:00
"rootDirectory" : "%s"
2021-12-28 08:29:30 -05:00
} ,
"http" : {
"address" : "127.0.0.1" ,
"port" : "%s"
} ,
"log" : {
"level" : "debug" ,
"output" : "%s"
} ,
"extensions" : {
"sync" : {
"enable" : true ,
"registries" : [ {
"urls" : [ "http://localhost:8080" ] ,
"tlsVerify" : false ,
"onDemand" : true ,
"maxRetries" : 3 ,
"retryDelay" : "15m" ,
"certDir" : "" ,
"content" : [
{
"prefix" : "zot-test" ,
"tags" : {
"regex" : ".*" ,
"semver" : true
}
}
]
} ]
}
}
2022-06-30 10:43:31 -05:00
} `
2021-12-28 08:29:30 -05:00
2022-09-02 08:40:34 -05:00
logPath , err := runCLIWithConfig ( t . TempDir ( ) , content )
2021-12-28 08:29:30 -05:00
So ( err , ShouldBeNil )
2022-09-02 08:40:34 -05:00
data , err := os . ReadFile ( logPath )
So ( err , ShouldBeNil )
defer os . Remove ( logPath ) // clean up
So ( string ( data ) , ShouldContainSubstring ,
2021-12-28 08:29:30 -05:00
"\"Extensions\":{\"Search\":null,\"Sync\":{\"Enable\":true" )
} )
Convey ( "sync explicitly disabled" , t , func ( c C ) {
2022-06-30 10:43:31 -05:00
content := ` {
2021-12-28 08:29:30 -05:00
"storage" : {
2022-06-30 10:43:31 -05:00
"rootDirectory" : "%s"
2021-12-28 08:29:30 -05:00
} ,
"http" : {
"address" : "127.0.0.1" ,
"port" : "%s"
} ,
"log" : {
"level" : "debug" ,
"output" : "%s"
} ,
"extensions" : {
"sync" : {
"enable" : false ,
"registries" : [ {
"urls" : [ "http://127.0.0.1:8080" ] ,
"tlsVerify" : false ,
"certDir" : "" ,
"maxRetries" : 3 ,
"retryDelay" : "15m"
} ]
}
}
2022-06-30 10:43:31 -05:00
} `
2021-12-28 08:29:30 -05:00
2022-09-02 08:40:34 -05:00
logPath , err := runCLIWithConfig ( t . TempDir ( ) , content )
So ( err , ShouldBeNil )
data , err := os . ReadFile ( logPath )
2021-12-28 08:29:30 -05:00
So ( err , ShouldBeNil )
2022-09-02 08:40:34 -05:00
defer os . Remove ( logPath ) // clean up
So ( string ( data ) , ShouldContainSubstring ,
2021-12-28 08:29:30 -05:00
"\"Extensions\":{\"Search\":null,\"Sync\":{\"Enable\":false" )
} )
}
2022-03-04 02:37:06 -05:00
func TestServeScrubExtension ( t * testing . T ) {
oldArgs := os . Args
defer func ( ) { os . Args = oldArgs } ( )
2022-09-27 20:06:50 -05:00
Convey ( "scrub implicitly enabled" , t , func ( c C ) {
content := ` {
"storage" : {
"rootDirectory" : "%s"
} ,
"http" : {
"address" : "127.0.0.1" ,
"port" : "%s"
} ,
"log" : {
"level" : "debug" ,
"output" : "%s"
} ,
"extensions" : {
"scrub" : {
}
}
} `
logPath , err := runCLIWithConfig ( t . TempDir ( ) , content )
So ( err , ShouldBeNil )
data , err := os . ReadFile ( logPath )
So ( err , ShouldBeNil )
defer os . Remove ( logPath ) // clean up
dataStr := string ( data )
So ( dataStr , ShouldContainSubstring ,
"\"Extensions\":{\"Search\":null,\"Sync\":null,\"Metrics\":null,\"Scrub\":{\"Enable\":true,\"Interval\":86400000000000},\"Lint\":null" ) //nolint:lll // gofumpt conflicts with lll
So ( dataStr , ShouldNotContainSubstring ,
"Scrub interval set to too-short interval < 2h, changing scrub duration to 2 hours and continuing." )
} )
Convey ( "scrub implicitly enabled, but with scrub interval param set" , t , func ( c C ) {
2022-06-30 10:43:31 -05:00
content := ` {
2022-03-04 02:37:06 -05:00
"storage" : {
2022-06-30 10:43:31 -05:00
"rootDirectory" : "%s"
2022-03-04 02:37:06 -05:00
} ,
"http" : {
"address" : "127.0.0.1" ,
"port" : "%s"
} ,
"log" : {
"level" : "debug" ,
"output" : "%s"
} ,
"extensions" : {
"scrub" : {
"interval" : "1h"
}
}
2022-06-30 10:43:31 -05:00
} `
2022-03-04 02:37:06 -05:00
2022-09-02 08:40:34 -05:00
logPath , err := runCLIWithConfig ( t . TempDir ( ) , content )
So ( err , ShouldBeNil )
data , err := os . ReadFile ( logPath )
2022-03-04 02:37:06 -05:00
So ( err , ShouldBeNil )
2022-09-02 08:40:34 -05:00
defer os . Remove ( logPath ) // clean up
2022-03-04 02:37:06 -05:00
// Even if in config we specified scrub interval=1h, the minimum interval is 2h
2022-09-02 08:40:34 -05:00
dataStr := string ( data )
So ( dataStr , ShouldContainSubstring ,
2022-09-27 20:06:50 -05:00
"\"Extensions\":{\"Search\":null,\"Sync\":null,\"Metrics\":null,\"Scrub\":{\"Enable\":true,\"Interval\":3600000000000},\"Lint\":null" ) //nolint:lll // gofumpt conflicts with lll
So ( dataStr , ShouldContainSubstring ,
"Scrub interval set to too-short interval < 2h, changing scrub duration to 2 hours and continuing." )
} )
Convey ( "scrub explicitly enabled, but without scrub interval param set" , t , func ( c C ) {
content := ` {
"storage" : {
"rootDirectory" : "%s"
} ,
"http" : {
"address" : "127.0.0.1" ,
"port" : "%s"
} ,
"log" : {
"level" : "debug" ,
"output" : "%s"
} ,
"extensions" : {
"scrub" : {
"enable" : true
}
}
} `
logPath , err := runCLIWithConfig ( t . TempDir ( ) , content )
So ( err , ShouldBeNil )
data , err := os . ReadFile ( logPath )
So ( err , ShouldBeNil )
defer os . Remove ( logPath ) // clean up
dataStr := string ( data )
2022-09-02 08:40:34 -05:00
So ( dataStr , ShouldContainSubstring ,
2022-09-27 20:06:50 -05:00
"\"Extensions\":{\"Search\":null,\"Sync\":null,\"Metrics\":null,\"Scrub\":{\"Enable\":true,\"Interval\":86400000000000},\"Lint\":null" ) //nolint:lll // gofumpt conflicts with lll
So ( dataStr , ShouldNotContainSubstring ,
2022-03-04 02:37:06 -05:00
"Scrub interval set to too-short interval < 2h, changing scrub duration to 2 hours and continuing." )
} )
2022-09-27 20:06:50 -05:00
Convey ( "scrub explicitly disabled" , t , func ( c C ) {
2022-06-30 10:43:31 -05:00
content := ` {
2022-03-04 02:37:06 -05:00
"storage" : {
2022-06-30 10:43:31 -05:00
"rootDirectory" : "%s"
2022-03-04 02:37:06 -05:00
} ,
"http" : {
"address" : "127.0.0.1" ,
"port" : "%s"
} ,
"log" : {
"level" : "debug" ,
"output" : "%s"
} ,
"extensions" : {
"scrub" : {
2022-09-27 20:06:50 -05:00
"enable" : false
2022-03-04 02:37:06 -05:00
}
}
2022-06-30 10:43:31 -05:00
} `
2022-03-04 02:37:06 -05:00
2022-09-02 08:40:34 -05:00
logPath , err := runCLIWithConfig ( t . TempDir ( ) , content )
So ( err , ShouldBeNil )
data , err := os . ReadFile ( logPath )
2022-03-04 02:37:06 -05:00
So ( err , ShouldBeNil )
2022-09-02 08:40:34 -05:00
defer os . Remove ( logPath ) // clean up
dataStr := string ( data )
So ( dataStr , ShouldContainSubstring ,
2022-09-27 20:06:50 -05:00
"\"Extensions\":{\"Search\":null,\"Sync\":null,\"Metrics\":null,\"Scrub\":{\"Enable\":false,\"Interval\":86400000000000},\"Lint\":null}" ) //nolint:lll // gofumpt conflicts with lll
2022-09-02 08:40:34 -05:00
So ( dataStr , ShouldContainSubstring , "Scrub config not provided, skipping scrub" )
So ( dataStr , ShouldNotContainSubstring ,
2022-03-04 02:37:06 -05:00
"Scrub interval set to too-short interval < 2h, changing scrub duration to 2 hours and continuing." )
} )
}
2022-06-24 08:08:47 -05:00
func TestServeLintExtension ( t * testing . T ) {
oldArgs := os . Args
defer func ( ) { os . Args = oldArgs } ( )
Convey ( "lint enabled" , t , func ( c C ) {
content := ` {
"storage" : {
"rootDirectory" : "%s"
} ,
"http" : {
"address" : "127.0.0.1" ,
"port" : "%s"
} ,
"log" : {
"level" : "debug" ,
"output" : "%s"
} ,
"extensions" : {
"lint" : {
2022-10-21 07:33:54 -05:00
"enable" : "true" ,
2022-06-24 08:08:47 -05:00
"mandatoryAnnotations" : [ "annot1" ]
}
}
} `
2022-09-02 08:40:34 -05:00
logPath , err := runCLIWithConfig ( t . TempDir ( ) , content )
2022-06-24 08:08:47 -05:00
So ( err , ShouldBeNil )
2022-09-02 08:40:34 -05:00
data , err := os . ReadFile ( logPath )
So ( err , ShouldBeNil )
defer os . Remove ( logPath ) // clean up
So ( string ( data ) , ShouldContainSubstring ,
2022-10-21 07:33:54 -05:00
"\"Extensions\":{\"Search\":null,\"Sync\":null,\"Metrics\":null,\"Scrub\":null,\"Lint\":{\"Enable\":true,\"MandatoryAnnotations\":" ) //nolint:lll // gofumpt conflicts with lll
2022-06-24 08:08:47 -05:00
} )
Convey ( "lint enabled" , t , func ( c C ) {
content := ` {
"storage" : {
"rootDirectory" : "%s"
} ,
"http" : {
"address" : "127.0.0.1" ,
"port" : "%s"
} ,
"log" : {
"level" : "debug" ,
"output" : "%s"
} ,
"extensions" : {
"lint" : {
2022-10-21 07:33:54 -05:00
"enable" : "false"
2022-06-24 08:08:47 -05:00
}
}
} `
2022-09-02 08:40:34 -05:00
logPath , err := runCLIWithConfig ( t . TempDir ( ) , content )
2022-06-24 08:08:47 -05:00
So ( err , ShouldBeNil )
2022-09-02 08:40:34 -05:00
data , err := os . ReadFile ( logPath )
So ( err , ShouldBeNil )
defer os . Remove ( logPath ) // clean up
So ( string ( data ) , ShouldContainSubstring ,
2022-10-21 07:33:54 -05:00
"\"Extensions\":{\"Search\":null,\"Sync\":null,\"Metrics\":null,\"Scrub\":null,\"Lint\":{\"Enable\":false,\"MandatoryAnnotations\":null}" ) //nolint:lll // gofumpt conflicts with lll
2022-06-24 08:08:47 -05:00
} )
}
2022-06-30 10:43:31 -05:00
func TestServeSearchEnabled ( t * testing . T ) {
2021-12-28 08:29:30 -05:00
oldArgs := os . Args
defer func ( ) { os . Args = oldArgs } ( )
Convey ( "search implicitly enabled" , t , func ( c C ) {
2022-06-30 10:43:31 -05:00
content := ` {
2021-12-28 08:29:30 -05:00
"storage" : {
2022-05-03 14:43:33 -05:00
"rootDirectory" : "%s"
2021-12-28 08:29:30 -05:00
} ,
"http" : {
"address" : "127.0.0.1" ,
"port" : "%s"
} ,
"log" : {
"level" : "debug" ,
"output" : "%s"
} ,
"extensions" : {
"search" : {
}
}
2022-06-30 10:43:31 -05:00
} `
2021-12-28 08:29:30 -05:00
2022-07-14 05:03:25 -05:00
tempDir := t . TempDir ( )
2022-09-02 08:40:34 -05:00
logPath , err := runCLIWithConfig ( tempDir , content )
So ( err , ShouldBeNil )
2022-07-14 05:03:25 -05:00
// to avoid data race when multiple go routines write to trivy DB instance.
2022-09-02 08:40:34 -05:00
defer os . Remove ( logPath ) // clean up
2022-11-30 02:35:06 -05:00
substring := "\"Extensions\":{\"Search\":{\"Enable\":true,\"CVE\":null},\"Sync\":null,\"Metrics\":null,\"Scrub\":null,\"Lint\":null}" //nolint:lll // gofumpt conflicts with lll
2022-09-02 08:40:34 -05:00
found , err := readLogFileAndSearchString ( logPath , substring , readLogFileTimeout )
So ( found , ShouldBeTrue )
So ( err , ShouldBeNil )
2021-12-28 08:29:30 -05:00
} )
2022-06-30 10:43:31 -05:00
}
2021-12-28 08:29:30 -05:00
2022-06-30 10:43:31 -05:00
func TestServeSearchEnabledCVE ( t * testing . T ) {
oldArgs := os . Args
2021-12-28 08:29:30 -05:00
2022-06-30 10:43:31 -05:00
defer func ( ) { os . Args = oldArgs } ( )
Convey ( "search implicitly enabled with CVE param set" , t , func ( c C ) {
content := ` {
2021-12-28 08:29:30 -05:00
"storage" : {
2022-05-03 14:43:33 -05:00
"rootDirectory" : "%s"
2021-12-28 08:29:30 -05:00
} ,
"http" : {
"address" : "127.0.0.1" ,
"port" : "%s"
} ,
"log" : {
"level" : "debug" ,
"output" : "%s"
} ,
"extensions" : {
"search" : {
"cve" : {
"updateInterval" : "1h"
}
}
}
2022-06-30 10:43:31 -05:00
} `
2021-12-28 08:29:30 -05:00
2022-07-14 05:03:25 -05:00
tempDir := t . TempDir ( )
2022-09-02 08:40:34 -05:00
logPath , err := runCLIWithConfig ( tempDir , content )
So ( err , ShouldBeNil )
defer os . Remove ( logPath ) // clean up
2022-07-14 05:03:25 -05:00
// to avoid data race when multiple go routines write to trivy DB instance.
WaitTillTrivyDBDownloadStarted ( tempDir )
2022-09-02 08:40:34 -05:00
2022-10-21 07:33:54 -05:00
substring := "\"Extensions\":{\"Search\":{\"Enable\":true,\"CVE\":{\"UpdateInterval\":3600000000000}},\"Sync\":null,\"Metrics\":null,\"Scrub\":null,\"Lint\":null}" //nolint:lll // gofumpt conflicts with lll
2022-09-02 08:40:34 -05:00
found , err := readLogFileAndSearchString ( logPath , substring , readLogFileTimeout )
So ( found , ShouldBeTrue )
So ( err , ShouldBeNil )
found , err = readLogFileAndSearchString ( logPath , "updating the CVE database" , readLogFileTimeout )
So ( found , ShouldBeTrue )
So ( err , ShouldBeNil )
substring = "CVE update interval set to too-short interval < 2h, changing update duration to 2 hours and continuing." //nolint:lll // gofumpt conflicts with lll
found , err = readLogFileAndSearchString ( logPath , substring , readLogFileTimeout )
So ( found , ShouldBeTrue )
2021-12-28 08:29:30 -05:00
So ( err , ShouldBeNil )
} )
2022-06-30 10:43:31 -05:00
}
2021-12-28 08:29:30 -05:00
2022-06-30 10:43:31 -05:00
func TestServeSearchEnabledNoCVE ( t * testing . T ) {
oldArgs := os . Args
2021-12-28 08:29:30 -05:00
2022-06-30 10:43:31 -05:00
defer func ( ) { os . Args = oldArgs } ( )
Convey ( "search explicitly enabled, but CVE parameter not set" , t , func ( c C ) {
content := ` {
2021-12-28 08:29:30 -05:00
"storage" : {
2022-05-03 14:43:33 -05:00
"rootDirectory" : "%s"
2021-12-28 08:29:30 -05:00
} ,
"http" : {
"address" : "127.0.0.1" ,
"port" : "%s"
} ,
"log" : {
"level" : "debug" ,
"output" : "%s"
} ,
"extensions" : {
"search" : {
"enable" : true
}
}
2022-06-30 10:43:31 -05:00
} `
2021-12-28 08:29:30 -05:00
2022-07-14 05:03:25 -05:00
tempDir := t . TempDir ( )
2022-09-02 08:40:34 -05:00
logPath , err := runCLIWithConfig ( tempDir , content )
So ( err , ShouldBeNil )
defer os . Remove ( logPath ) // clean up
2022-11-30 02:35:06 -05:00
substring := "\"Extensions\":{\"Search\":{\"Enable\":true,\"CVE\":null},\"Sync\":null,\"Metrics\":null,\"Scrub\":null,\"Lint\":null}" //nolint:lll // gofumpt conflicts with lll
2022-09-02 08:40:34 -05:00
found , err := readLogFileAndSearchString ( logPath , substring , readLogFileTimeout )
So ( found , ShouldBeTrue )
So ( err , ShouldBeNil )
2021-12-28 08:29:30 -05:00
} )
2022-06-30 10:43:31 -05:00
}
2021-12-28 08:29:30 -05:00
2022-06-30 10:43:31 -05:00
func TestServeSearchDisabled ( t * testing . T ) {
oldArgs := os . Args
2021-12-28 08:29:30 -05:00
2022-06-30 10:43:31 -05:00
defer func ( ) { os . Args = oldArgs } ( )
Convey ( "search explicitly disabled" , t , func ( c C ) {
content := ` {
2021-12-28 08:29:30 -05:00
"storage" : {
2022-05-03 14:43:33 -05:00
"rootDirectory" : "%s"
2021-12-28 08:29:30 -05:00
} ,
"http" : {
"address" : "127.0.0.1" ,
"port" : "%s"
} ,
"log" : {
"level" : "debug" ,
"output" : "%s"
} ,
"extensions" : {
"search" : {
"enable" : false ,
"cve" : {
"updateInterval" : "3h"
}
}
}
2022-06-30 10:43:31 -05:00
} `
2021-12-28 08:29:30 -05:00
2022-09-02 08:40:34 -05:00
logPath , err := runCLIWithConfig ( t . TempDir ( ) , content )
2021-12-28 08:29:30 -05:00
So ( err , ShouldBeNil )
2022-09-02 08:40:34 -05:00
data , err := os . ReadFile ( logPath )
2021-12-28 08:29:30 -05:00
So ( err , ShouldBeNil )
2022-09-02 08:40:34 -05:00
defer os . Remove ( logPath ) // clean up
dataStr := string ( data )
So ( dataStr , ShouldContainSubstring ,
2022-10-21 07:33:54 -05:00
"\"Extensions\":{\"Search\":{\"Enable\":false,\"CVE\":{\"UpdateInterval\":10800000000000}},\"Sync\":null,\"Metrics\":null,\"Scrub\":null,\"Lint\":null}" ) //nolint:lll // gofumpt conflicts with lll
2022-09-02 08:40:34 -05:00
So ( dataStr , ShouldContainSubstring , "CVE config not provided, skipping CVE update" )
So ( dataStr , ShouldNotContainSubstring ,
2021-12-28 08:29:30 -05:00
"CVE update interval set to too-short interval < 2h, changing update duration to 2 hours and continuing." )
} )
}
2022-06-30 10:43:31 -05:00
2022-10-05 05:21:14 -05:00
func readLogFileAndSearchString ( logPath string , stringToMatch string , timeout time . Duration ) ( bool , error ) { //nolint:unparam,lll
2022-09-02 08:40:34 -05:00
ctx , cancelFunc := context . WithTimeout ( context . Background ( ) , timeout )
defer cancelFunc ( )
for {
select {
case <- ctx . Done ( ) :
return false , nil
default :
content , err := os . ReadFile ( logPath )
if err != nil {
return false , err
}
if strings . Contains ( string ( content ) , stringToMatch ) {
return true , nil
}
}
}
}
2022-06-30 10:43:31 -05:00
// run cli and return output.
func runCLIWithConfig ( tempDir string , config string ) ( string , error ) {
port := GetFreePort ( )
baseURL := GetBaseURL ( port )
2022-09-02 07:56:02 -05:00
logFile , err := os . CreateTemp ( tempDir , "zot-log*.txt" )
2022-06-30 10:43:31 -05:00
if err != nil {
return "" , err
}
2022-09-02 07:56:02 -05:00
cfgfile , err := os . CreateTemp ( tempDir , "zot-test*.json" )
2022-06-30 10:43:31 -05:00
if err != nil {
return "" , err
}
config = fmt . Sprintf ( config , tempDir , port , logFile . Name ( ) )
_ , err = cfgfile . Write ( [ ] byte ( config ) )
if err != nil {
return "" , err
}
err = cfgfile . Close ( )
if err != nil {
return "" , err
}
os . Args = [ ] string { "cli_test" , "serve" , cfgfile . Name ( ) }
go func ( ) {
err = cli . NewServerRootCmd ( ) . Execute ( )
if err != nil {
panic ( err )
}
} ( )
WaitTillServerReady ( baseURL )
2022-09-02 08:40:34 -05:00
return logFile . Name ( ) , nil
2022-06-30 10:43:31 -05:00
}