2021-05-28 15:59:00 +00:00
|
|
|
// Copyright 2019 Drone IO, Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-05-27 13:58:23 +00:00
|
|
|
package converter
|
|
|
|
|
|
|
|
import (
|
2021-08-03 09:51:39 +00:00
|
|
|
"encoding/json"
|
2021-06-01 09:29:58 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
|
2021-05-27 13:58:23 +00:00
|
|
|
"github.com/drone/drone/core"
|
|
|
|
"github.com/drone/drone/mock"
|
2021-06-02 10:59:39 +00:00
|
|
|
|
2021-05-27 13:58:23 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
)
|
|
|
|
|
2021-06-03 13:06:57 +00:00
|
|
|
func TestTemplatePluginConvertStarlark(t *testing.T) {
|
2021-05-28 15:59:00 +00:00
|
|
|
templateArgs, err := ioutil.ReadFile("testdata/starlark.template.yml")
|
2021-05-27 13:58:23 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-28 15:59:00 +00:00
|
|
|
req := &core.ConvertArgs{
|
|
|
|
Build: &core.Build{
|
|
|
|
After: "3d21ec53a331a6f037a91c368710b99387d012c1",
|
|
|
|
},
|
|
|
|
Repo: &core.Repository{
|
|
|
|
Slug: "octocat/hello-world",
|
|
|
|
Config: ".drone.yml",
|
|
|
|
Namespace: "octocat",
|
|
|
|
},
|
|
|
|
Config: &core.Config{
|
|
|
|
Data: string(templateArgs),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeInput, err := ioutil.ReadFile("testdata/starlark.input.star")
|
2021-05-27 13:58:23 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-28 15:59:00 +00:00
|
|
|
after, err := ioutil.ReadFile("testdata/starlark.input.star.golden")
|
2021-05-27 13:58:23 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
template := &core.Template{
|
2021-06-01 15:29:14 +00:00
|
|
|
Name: "plugin.starlark",
|
|
|
|
Data: string(beforeInput),
|
|
|
|
Namespace: "octocat",
|
2021-05-27 13:58:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
controller := gomock.NewController(t)
|
|
|
|
defer controller.Finish()
|
|
|
|
|
|
|
|
templates := mock.NewMockTemplateStore(controller)
|
2021-05-28 15:59:00 +00:00
|
|
|
templates.EXPECT().FindName(gomock.Any(), template.Name, req.Repo.Namespace).Return(template, nil)
|
2021-05-27 13:58:23 +00:00
|
|
|
|
2021-05-27 14:18:21 +00:00
|
|
|
plugin := Template(templates)
|
2021-05-27 13:58:23 +00:00
|
|
|
config, err := plugin.Convert(noContext, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if config == nil {
|
|
|
|
t.Error("Want non-nil configuration")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if want, got := config.Data, string(after); want != got {
|
|
|
|
t.Errorf("Want %q got %q", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplatePluginConvertNotYamlFile(t *testing.T) {
|
|
|
|
|
2021-05-27 14:18:21 +00:00
|
|
|
plugin := Template(nil)
|
2021-05-27 13:58:23 +00:00
|
|
|
req := &core.ConvertArgs{
|
|
|
|
Build: &core.Build{
|
|
|
|
After: "3d21ec53a331a6f037a91c368710b99387d012c1",
|
|
|
|
},
|
|
|
|
Repo: &core.Repository{
|
|
|
|
Slug: "octocat/hello-world",
|
|
|
|
Config: ".drone.star",
|
|
|
|
},
|
|
|
|
Config: &core.Config{},
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := plugin.Convert(noContext, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if config != nil {
|
|
|
|
t.Errorf("Expect nil config returned for non-starlark files")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplatePluginConvertDroneFileTypePipeline(t *testing.T) {
|
|
|
|
args, err := ioutil.ReadFile("testdata/drone.yml")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2021-05-27 14:18:21 +00:00
|
|
|
plugin := Template(nil)
|
2021-05-27 13:58:23 +00:00
|
|
|
req := &core.ConvertArgs{
|
|
|
|
Build: &core.Build{
|
|
|
|
After: "3d21ec53a331a6f037a91c368710b99387d012c1",
|
|
|
|
},
|
|
|
|
Repo: &core.Repository{
|
|
|
|
Slug: "octocat/hello-world",
|
|
|
|
Config: ".drone.yml",
|
|
|
|
},
|
|
|
|
Config: &core.Config{Data: string(args)},
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := plugin.Convert(noContext, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if config != nil {
|
|
|
|
t.Errorf("Expect nil config returned for non-starlark files")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplatePluginConvertTemplateNotFound(t *testing.T) {
|
|
|
|
templateArgs, err := ioutil.ReadFile("testdata/starlark.template.yml")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &core.ConvertArgs{
|
|
|
|
Build: &core.Build{
|
|
|
|
After: "3d21ec53a331a6f037a91c368710b99387d012c1",
|
|
|
|
},
|
|
|
|
Repo: &core.Repository{
|
2021-05-28 15:59:00 +00:00
|
|
|
Slug: "octocat/hello-world",
|
|
|
|
Config: ".drone.yml",
|
|
|
|
Namespace: "octocat",
|
2021-05-27 13:58:23 +00:00
|
|
|
},
|
|
|
|
Config: &core.Config{Data: string(templateArgs)},
|
|
|
|
}
|
|
|
|
|
2021-05-28 15:59:00 +00:00
|
|
|
controller := gomock.NewController(t)
|
|
|
|
defer controller.Finish()
|
|
|
|
|
|
|
|
template := &core.Template{
|
|
|
|
Name: "plugin.starlark",
|
|
|
|
Data: "",
|
|
|
|
}
|
|
|
|
|
|
|
|
templates := mock.NewMockTemplateStore(controller)
|
|
|
|
templates.EXPECT().FindName(gomock.Any(), template.Name, req.Repo.Namespace).Return(nil, nil)
|
|
|
|
|
|
|
|
plugin := Template(templates)
|
|
|
|
|
2021-05-27 13:58:23 +00:00
|
|
|
config, err := plugin.Convert(noContext, req)
|
|
|
|
if config != nil {
|
|
|
|
t.Errorf("template converter: template name given not found")
|
|
|
|
}
|
|
|
|
}
|
2021-06-03 13:06:57 +00:00
|
|
|
|
|
|
|
func TestTemplatePluginConvertJsonnet(t *testing.T) {
|
|
|
|
templateArgs, err := ioutil.ReadFile("testdata/jsonnet.template.yml")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &core.ConvertArgs{
|
|
|
|
Build: &core.Build{
|
|
|
|
After: "3d21ec53a331a6f037a91c368710b99387d012c1",
|
|
|
|
},
|
|
|
|
Repo: &core.Repository{
|
|
|
|
Slug: "octocat/hello-world",
|
|
|
|
Config: ".drone.yml",
|
|
|
|
Namespace: "octocat",
|
|
|
|
},
|
|
|
|
Config: &core.Config{
|
|
|
|
Data: string(templateArgs),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeInput, err := ioutil.ReadFile("testdata/input.jsonnet")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
after, err := ioutil.ReadFile("testdata/input.jsonnet.golden")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
template := &core.Template{
|
|
|
|
Name: "plugin.jsonnet",
|
|
|
|
Data: string(beforeInput),
|
|
|
|
Namespace: "octocat",
|
|
|
|
}
|
|
|
|
|
|
|
|
controller := gomock.NewController(t)
|
|
|
|
defer controller.Finish()
|
|
|
|
|
|
|
|
templates := mock.NewMockTemplateStore(controller)
|
|
|
|
templates.EXPECT().FindName(gomock.Any(), template.Name, req.Repo.Namespace).Return(template, nil)
|
|
|
|
|
|
|
|
plugin := Template(templates)
|
|
|
|
config, err := plugin.Convert(noContext, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if config == nil {
|
|
|
|
t.Error("Want non-nil configuration")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if want, got := config.Data, string(after); want != got {
|
|
|
|
t.Errorf("Want %q got %q", want, got)
|
|
|
|
}
|
|
|
|
}
|
2021-08-02 16:23:02 +00:00
|
|
|
|
|
|
|
func TestTemplateNestedValuesPluginConvertStarlark(t *testing.T) {
|
2021-08-03 09:51:39 +00:00
|
|
|
type Pipeline struct {
|
|
|
|
Kind string `json:"kind"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Steps []struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Image string `json:"image"`
|
|
|
|
Commands []string `json:"commands"`
|
|
|
|
} `json:"steps"`
|
|
|
|
}
|
|
|
|
|
2021-08-02 16:23:02 +00:00
|
|
|
templateArgs, err := ioutil.ReadFile("testdata/starlark-nested.template.yml")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &core.ConvertArgs{
|
|
|
|
Build: &core.Build{
|
|
|
|
After: "3d21ec53a331a6f037a91c368710b99387d012c1",
|
|
|
|
},
|
|
|
|
Repo: &core.Repository{
|
|
|
|
Slug: "octocat/hello-world",
|
|
|
|
Config: ".drone.yml",
|
|
|
|
Namespace: "octocat",
|
|
|
|
},
|
|
|
|
Config: &core.Config{
|
|
|
|
Data: string(templateArgs),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeInput, err := ioutil.ReadFile("testdata/starlark.input-nested.star")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
after, err := ioutil.ReadFile("testdata/starlark.input-nested.star.golden")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
template := &core.Template{
|
|
|
|
Name: "test.nested.starlark",
|
|
|
|
Data: string(beforeInput),
|
|
|
|
Namespace: "octocat",
|
|
|
|
}
|
|
|
|
|
|
|
|
controller := gomock.NewController(t)
|
|
|
|
defer controller.Finish()
|
|
|
|
|
|
|
|
templates := mock.NewMockTemplateStore(controller)
|
|
|
|
templates.EXPECT().FindName(gomock.Any(), template.Name, req.Repo.Namespace).Return(template, nil)
|
|
|
|
|
|
|
|
plugin := Template(templates)
|
|
|
|
config, err := plugin.Convert(noContext, req)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if config == nil {
|
|
|
|
t.Error("Want non-nil configuration")
|
|
|
|
return
|
|
|
|
}
|
2021-08-03 09:51:39 +00:00
|
|
|
result := Pipeline{}
|
|
|
|
err = json.Unmarshal(after, &result)
|
|
|
|
beforeConfig := Pipeline{}
|
|
|
|
err = json.Unmarshal([]byte(config.Data), &beforeConfig)
|
2021-08-02 16:23:02 +00:00
|
|
|
|
2021-08-03 09:51:39 +00:00
|
|
|
if want, got := beforeConfig.Name, result.Name; want != got {
|
|
|
|
t.Errorf("Want %q got %q", want, got)
|
|
|
|
}
|
|
|
|
if want, got := beforeConfig.Kind, result.Kind; want != got {
|
|
|
|
t.Errorf("Want %q got %q", want, got)
|
|
|
|
}
|
|
|
|
if want, got := beforeConfig.Steps[0].Name, result.Steps[0].Name; want != got {
|
|
|
|
t.Errorf("Want %q got %q", want, got)
|
|
|
|
}
|
|
|
|
if want, got := beforeConfig.Steps[0].Commands[0], result.Steps[0].Commands[0]; want != got {
|
|
|
|
t.Errorf("Want %q got %q", want, got)
|
|
|
|
}
|
|
|
|
if want, got := beforeConfig.Steps[0].Image, result.Steps[0].Image; want != got {
|
2021-08-02 16:23:02 +00:00
|
|
|
t.Errorf("Want %q got %q", want, got)
|
|
|
|
}
|
|
|
|
}
|