Skip to content

Commit

Permalink
New unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wbreza committed Jul 31, 2023
1 parent 3b789dc commit ab85557
Show file tree
Hide file tree
Showing 6 changed files with 387 additions and 0 deletions.
54 changes: 54 additions & 0 deletions cli/azd/pkg/templates/file_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package templates

import (
"os"
"path/filepath"
"testing"

"github.com/azure/azure-dev/cli/azd/pkg/config"
"github.com/azure/azure-dev/cli/azd/pkg/osutil"
"github.com/stretchr/testify/require"
)

func Test_NewFileTemplateSource_FileExists(t *testing.T) {
name := "test"
configDir, err := config.GetUserConfigDir()
require.NoError(t, err)

path := filepath.Join(configDir, "test-templates.json")
jsonTemplates := `[{"name": "template1", "repositoryPath": "path/to/template1"}, {"name": "template2", "repositoryPath": "path/to/template2"}]`

Check failure on line 19 in cli/azd/pkg/templates/file_source_test.go

View workflow job for this annotation

GitHub Actions / azd-lint (ubuntu-latest)

line is 144 characters (lll)
err = os.WriteFile(path, []byte(jsonTemplates), osutil.PermissionFile)
require.Nil(t, err)

source, err := NewFileTemplateSource(name, path)
require.Nil(t, err)

require.Equal(t, name, source.Name())

err = os.Remove(path)
require.Nil(t, err)
}

func Test_NewFileTemplateSource_InvalidJson(t *testing.T) {
name := "test"
configDir, err := config.GetUserConfigDir()
require.NoError(t, err)

path := filepath.Join(configDir, "test-templates.json")
invalidJson := `invalid json`
err = os.WriteFile(path, []byte(invalidJson), osutil.PermissionFile)
require.Nil(t, err)

_, err = NewFileTemplateSource(name, path)
require.Error(t, err)

err = os.Remove(path)
require.Nil(t, err)
}

func Test_NewFileTemplateSource_FileDoesNotExist(t *testing.T) {
name := "test"
path := "testdata/nonexistent.json"
_, err := NewFileTemplateSource(name, path)
require.Error(t, err)
}
86 changes: 86 additions & 0 deletions cli/azd/pkg/templates/json_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package templates

import (
"context"
"encoding/json"
"testing"

"github.com/stretchr/testify/require"
)

var testTemplates []*Template = []*Template{
{
Name: "template1",
Description: "Description of template 1",
RepositoryPath: "path/to/template1",
},
{
Name: "template2",
Description: "Description of template 2",
RepositoryPath: "path/to/template2",
},
}

func jsonTemplates() string {
jsonTemplates, err := json.Marshal(testTemplates)
if err != nil {
panic(err)
}

return string(jsonTemplates)
}

func Test_NewJsonTemplateSource(t *testing.T) {
name := "test"
source, err := NewJsonTemplateSource(name, jsonTemplates())
require.Nil(t, err)

require.Equal(t, name, source.Name())
}

func Test_NewJsonTemplateSource_InvalidJson(t *testing.T) {
name := "test"
jsonTemplates := `invalid json`
_, err := NewJsonTemplateSource(name, jsonTemplates)
require.Error(t, err)
}

func Test_JsonTemplateSource_ListTemplates(t *testing.T) {
name := "test"
source, err := NewJsonTemplateSource(name, jsonTemplates())
require.Nil(t, err)

templates, err := source.ListTemplates(context.Background())
require.Nil(t, err)
require.Equal(t, 2, len(templates))

expectedTemplate1 := &Template{Name: "template1", RepositoryPath: "path/to/template1", Source: name}
require.Equal(t, expectedTemplate1, templates[0])

expectedTemplate2 := &Template{Name: "template2", RepositoryPath: "path/to/template2", Source: name}
require.Equal(t, expectedTemplate2, templates[1])
}

func Test_JsonTemplateSource_GetTemplate_MatchFound(t *testing.T) {
name := "test"
source, err := NewJsonTemplateSource(name, jsonTemplates())
require.Nil(t, err)

template, err := source.GetTemplate(context.Background(), "path/to/template1")
require.Nil(t, err)

expectedTemplate := &Template{Name: "template1", RepositoryPath: "path/to/template1", Source: name}
require.Equal(t, expectedTemplate, template)
}

func Test_JsonTemplateSource_GetTemplate_NoMatchFound(t *testing.T) {
name := "test"
source, err := NewJsonTemplateSource(name, jsonTemplates())
require.Nil(t, err)

template, err := source.GetTemplate(context.Background(), "path/to/template3")
require.Error(t, err)
require.ErrorIs(t, err, ErrTemplateNotFound)

require.Nil(t, template)
}
181 changes: 181 additions & 0 deletions cli/azd/pkg/templates/source_manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package templates

import (
"context"
"testing"

"github.com/azure/azure-dev/cli/azd/pkg/config"
"github.com/azure/azure-dev/cli/azd/test/mocks"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

func Test_sourceManager_List(t *testing.T) {
mockContext := mocks.NewMockContext(context.Background())
configManager := &mockUserConfigManager{}
sm := NewSourceManager(configManager, mockContext.HttpClient)

config := config.NewConfig(nil)
config.Set("template.sources", map[string]interface{}{

Check failure on line 19 in cli/azd/pkg/templates/source_manager_test.go

View workflow job for this annotation

GitHub Actions / azd-lint (ubuntu-latest)

Error return value of `config.Set` is not checked (errcheck)
"test": map[string]interface{}{
"type": "file",
"location": "testdata/templates.json",
},
})
configManager.On("Load").Return(config, nil)

sources, err := sm.List(context.Background())
require.Nil(t, err)

require.Len(t, sources, 1)
require.Equal(t, "test", sources[0].Key)
}

func Test_sourceManager_Get(t *testing.T) {
mockContext := mocks.NewMockContext(context.Background())
configManager := &mockUserConfigManager{}
sm := NewSourceManager(configManager, mockContext.HttpClient)

config := config.NewConfig(nil)
config.Set("template.sources", map[string]interface{}{

Check failure on line 40 in cli/azd/pkg/templates/source_manager_test.go

View workflow job for this annotation

GitHub Actions / azd-lint (ubuntu-latest)

Error return value of `config.Set` is not checked (errcheck)
"test": map[string]interface{}{
"type": "file",
"location": "testdata/templates.json",
},
})
configManager.On("Load").Return(config, nil)

source, err := sm.Get(context.Background(), "test")
require.Nil(t, err)
require.NotNil(t, source)

require.Equal(t, "test", source.Key)
}

func Test_sourceManager_Add(t *testing.T) {
mockContext := mocks.NewMockContext(context.Background())
configManager := &mockUserConfigManager{}
sm := NewSourceManager(configManager, mockContext.HttpClient)

config := config.NewConfig(nil)
config.Set("template.sources", map[string]interface{}{})

Check failure on line 61 in cli/azd/pkg/templates/source_manager_test.go

View workflow job for this annotation

GitHub Actions / azd-lint (ubuntu-latest)

Error return value of `config.Set` is not checked (errcheck)
configManager.On("Load").Return(config, nil)
configManager.On("Save", mock.Anything).Return(nil)

key := "test"
source := &SourceConfig{
Type: SourceFile,
Location: "testdata/templates.json",
}
err := sm.Add(context.Background(), key, source)
require.Nil(t, err)
}

func Test_sourceManager_Remove(t *testing.T) {
mockContext := mocks.NewMockContext(context.Background())
configManager := &mockUserConfigManager{}
sm := NewSourceManager(configManager, mockContext.HttpClient)

key := "test"
config := config.NewConfig(nil)
config.Set("template.sources.test", map[string]interface{}{})
configManager.On("Load").Return(config, nil)
configManager.On("Save", mock.Anything).Return(nil)

err := sm.Remove(context.Background(), key)
require.Nil(t, err)
}

func Test_sourceManager_CreateSource_InvalidType(t *testing.T) {
config := &SourceConfig{
Name: "test",
Type: "invalid",
}

mockContext := mocks.NewMockContext(context.Background())
configManager := &mockUserConfigManager{}
sm := NewSourceManager(configManager, mockContext.HttpClient)

_, err := sm.CreateSource(context.Background(), config)
require.NotNil(t, err)
}

func Test_sourceManager_CreateSource_InvalidLocation(t *testing.T) {
config := &SourceConfig{
Name: "test",
Type: SourceFile,
Location: "invalid",
}

mockContext := mocks.NewMockContext(context.Background())
configManager := &mockUserConfigManager{}
sm := NewSourceManager(configManager, mockContext.HttpClient)

_, err := sm.CreateSource(context.Background(), config)
require.NotNil(t, err)
}

func Test_sourceManager_CreateSource_File(t *testing.T) {
config := &SourceConfig{
Name: "test",
Type: SourceFile,
Location: "testdata/templates.json",
}

mockContext := mocks.NewMockContext(context.Background())
configManager := &mockUserConfigManager{}
sm := NewSourceManager(configManager, mockContext.HttpClient)

source, err := sm.CreateSource(context.Background(), config)
require.Nil(t, err)

require.Equal(t, config.Name, source.Name())
}

func Test_sourceManager_CreateSource_Url(t *testing.T) {
config := &SourceConfig{
Name: "test",
Type: SourceUrl,
Location: "https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore",
}

mockContext := mocks.NewMockContext(context.Background())
configManager := &mockUserConfigManager{}
sm := NewSourceManager(configManager, mockContext.HttpClient)

source, err := sm.CreateSource(context.Background(), config)
require.Nil(t, err)

require.Equal(t, config.Name, source.Name())
}

func Test_sourceManager_CreateSource_Resource(t *testing.T) {
config := &SourceConfig{
Name: "test",
Type: SourceResource,
Location: "",
}

mockContext := mocks.NewMockContext(context.Background())
configManager := &mockUserConfigManager{}
sm := NewSourceManager(configManager, mockContext.HttpClient)

source, err := sm.CreateSource(context.Background(), config)
require.Nil(t, err)

require.Equal(t, config.Name, source.Name())
}

type mockUserConfigManager struct {
mock.Mock
}

func (m *mockUserConfigManager) Load() (config.Config, error) {
args := m.Called()
return args.Get(0).(config.Config), args.Error(1)
}

func (m *mockUserConfigManager) Save(config config.Config) error {
args := m.Called(config)
return args.Error(0)
}
62 changes: 62 additions & 0 deletions cli/azd/pkg/templates/url_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package templates

import (
"context"
"net/http"
"testing"

"github.com/azure/azure-dev/cli/azd/test/mocks"
"github.com/stretchr/testify/require"
)

func Test_NewUrlTemplateSource_ValidUrl(t *testing.T) {
mockContext := mocks.NewMockContext(context.Background())

name := "test"
url := "https://example.com/templates.json"

mockContext.HttpClient.When(func(req *http.Request) bool {
return req.Method == http.MethodGet && req.URL.String() == url
}).RespondFn(func(req *http.Request) (*http.Response, error) {
return mocks.CreateHttpResponseWithBody(req, http.StatusOK, testTemplates)
})

source, err := NewUrlTemplateSource(context.Background(), name, url, mockContext.HttpClient)
require.Nil(t, err)

require.Equal(t, name, source.Name())
}

func Test_NewUrlTemplateSource_ValidUrl_InvalidJson(t *testing.T) {
mockContext := mocks.NewMockContext(context.Background())

name := "test"
url := "https://example.com/templates.json"

mockContext.HttpClient.When(func(req *http.Request) bool {
return req.Method == http.MethodGet && req.URL.String() == url
}).RespondFn(func(req *http.Request) (*http.Response, error) {
return mocks.CreateHttpResponseWithBody(req, http.StatusOK, "invalid json")
})

source, err := NewUrlTemplateSource(context.Background(), name, url, mockContext.HttpClient)
require.Nil(t, source)
require.Error(t, err)
}

func Test_NewUrlTemplateSource_InvalidUrl(t *testing.T) {
mockContext := mocks.NewMockContext(context.Background())

name := "test"
url := "https://example.com/templates.json"

mockContext.HttpClient.When(func(req *http.Request) bool {
return req.Method == http.MethodGet && req.URL.String() == url
}).RespondFn(func(req *http.Request) (*http.Response, error) {
return mocks.CreateEmptyHttpResponse(req, http.StatusNotFound)
})

source, err := NewUrlTemplateSource(context.Background(), name, url, mockContext.HttpClient)
require.Nil(t, source)
require.Error(t, err)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ require github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/appcontainers/arma

require github.com/adam-lavrik/go-imath v0.0.0-20210910152346-265a42a96f0b

require github.com/stretchr/objx v0.5.0 // indirect

require (
github.com/Azure/azure-pipeline-go v0.2.1 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0 // indirect
Expand Down

0 comments on commit ab85557

Please sign in to comment.