2018-12-20 14:22:03 -05:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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-11-30 16:39:47 -05:00
|
|
|
//go:build !nodeploy
|
2020-10-23 03:03:41 -04:00
|
|
|
// +build !nodeploy
|
|
|
|
|
2018-12-20 14:22:03 -05:00
|
|
|
package deploy
|
|
|
|
|
|
|
|
import (
|
2019-05-31 12:55:48 -04:00
|
|
|
"fmt"
|
2018-12-20 14:22:03 -05:00
|
|
|
"testing"
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
qt "github.com/frankban/quicktest"
|
2018-12-20 14:22:03 -05:00
|
|
|
"github.com/gohugoio/hugo/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDecodeConfigFromTOML(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2018-12-20 14:22:03 -05:00
|
|
|
|
|
|
|
tomlConfig := `
|
|
|
|
|
|
|
|
someOtherValue = "foo"
|
|
|
|
|
|
|
|
[deployment]
|
2019-05-03 12:30:46 -04:00
|
|
|
|
|
|
|
order = ["o1", "o2"]
|
|
|
|
|
2019-05-31 12:55:48 -04:00
|
|
|
# All lowercase.
|
2018-12-20 14:22:03 -05:00
|
|
|
[[deployment.targets]]
|
2019-05-31 12:55:48 -04:00
|
|
|
name = "name0"
|
|
|
|
url = "url0"
|
|
|
|
cloudfrontdistributionid = "cdn0"
|
2020-02-27 01:26:05 -05:00
|
|
|
include = "*.html"
|
2019-05-31 12:55:48 -04:00
|
|
|
|
|
|
|
# All uppercase.
|
|
|
|
[[deployment.targets]]
|
|
|
|
NAME = "name1"
|
2018-12-20 14:22:03 -05:00
|
|
|
URL = "url1"
|
2019-05-31 12:55:48 -04:00
|
|
|
CLOUDFRONTDISTRIBUTIONID = "cdn1"
|
2020-02-27 01:26:05 -05:00
|
|
|
INCLUDE = "*.jpg"
|
2019-05-01 16:25:06 -04:00
|
|
|
|
2019-05-31 12:55:48 -04:00
|
|
|
# Camelcase.
|
2018-12-20 14:22:03 -05:00
|
|
|
[[deployment.targets]]
|
|
|
|
name = "name2"
|
|
|
|
url = "url2"
|
2019-05-31 12:55:48 -04:00
|
|
|
cloudFrontDistributionID = "cdn2"
|
2020-02-27 01:26:05 -05:00
|
|
|
exclude = "*.png"
|
2018-12-20 14:22:03 -05:00
|
|
|
|
2019-05-31 12:55:48 -04:00
|
|
|
# All lowercase.
|
2018-12-20 14:22:03 -05:00
|
|
|
[[deployment.matchers]]
|
2019-05-31 12:55:48 -04:00
|
|
|
pattern = "^pattern0$"
|
|
|
|
cachecontrol = "cachecontrol0"
|
|
|
|
contentencoding = "contentencoding0"
|
|
|
|
contenttype = "contenttype0"
|
2018-12-20 14:22:03 -05:00
|
|
|
|
2019-05-31 12:55:48 -04:00
|
|
|
# All uppercase.
|
|
|
|
[[deployment.matchers]]
|
|
|
|
PATTERN = "^pattern1$"
|
|
|
|
CACHECONTROL = "cachecontrol1"
|
|
|
|
CONTENTENCODING = "contentencoding1"
|
|
|
|
CONTENTTYPE = "contenttype1"
|
|
|
|
GZIP = true
|
|
|
|
FORCE = true
|
|
|
|
|
|
|
|
# Camelcase.
|
2018-12-20 14:22:03 -05:00
|
|
|
[[deployment.matchers]]
|
|
|
|
pattern = "^pattern2$"
|
2019-05-31 12:55:48 -04:00
|
|
|
cacheControl = "cachecontrol2"
|
|
|
|
contentEncoding = "contentencoding2"
|
|
|
|
contentType = "contenttype2"
|
|
|
|
gzip = true
|
|
|
|
force = true
|
2018-12-20 14:22:03 -05:00
|
|
|
`
|
|
|
|
cfg, err := config.FromConfigString(tomlConfig, "toml")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2018-12-20 14:22:03 -05:00
|
|
|
|
|
|
|
dcfg, err := decodeConfig(cfg)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2018-12-20 14:22:03 -05:00
|
|
|
|
2019-05-31 12:55:48 -04:00
|
|
|
// Order.
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(len(dcfg.Order), qt.Equals, 2)
|
|
|
|
c.Assert(dcfg.Order[0], qt.Equals, "o1")
|
|
|
|
c.Assert(dcfg.Order[1], qt.Equals, "o2")
|
|
|
|
c.Assert(len(dcfg.ordering), qt.Equals, 2)
|
2019-05-03 12:30:46 -04:00
|
|
|
|
2019-05-31 12:55:48 -04:00
|
|
|
// Targets.
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(len(dcfg.Targets), qt.Equals, 3)
|
2020-02-27 01:26:05 -05:00
|
|
|
wantInclude := []string{"*.html", "*.jpg", ""}
|
|
|
|
wantExclude := []string{"", "", "*.png"}
|
2019-05-31 12:55:48 -04:00
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
tgt := dcfg.Targets[i]
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(tgt.Name, qt.Equals, fmt.Sprintf("name%d", i))
|
|
|
|
c.Assert(tgt.URL, qt.Equals, fmt.Sprintf("url%d", i))
|
|
|
|
c.Assert(tgt.CloudFrontDistributionID, qt.Equals, fmt.Sprintf("cdn%d", i))
|
2020-02-27 01:26:05 -05:00
|
|
|
c.Assert(tgt.Include, qt.Equals, wantInclude[i])
|
|
|
|
if wantInclude[i] != "" {
|
|
|
|
c.Assert(tgt.includeGlob, qt.Not(qt.IsNil))
|
|
|
|
}
|
|
|
|
c.Assert(tgt.Exclude, qt.Equals, wantExclude[i])
|
|
|
|
if wantExclude[i] != "" {
|
|
|
|
c.Assert(tgt.excludeGlob, qt.Not(qt.IsNil))
|
|
|
|
}
|
2019-05-31 12:55:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Matchers.
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(len(dcfg.Matchers), qt.Equals, 3)
|
2019-05-31 12:55:48 -04:00
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
m := dcfg.Matchers[i]
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(m.Pattern, qt.Equals, fmt.Sprintf("^pattern%d$", i))
|
|
|
|
c.Assert(m.re, qt.Not(qt.IsNil))
|
|
|
|
c.Assert(m.CacheControl, qt.Equals, fmt.Sprintf("cachecontrol%d", i))
|
|
|
|
c.Assert(m.ContentEncoding, qt.Equals, fmt.Sprintf("contentencoding%d", i))
|
|
|
|
c.Assert(m.ContentType, qt.Equals, fmt.Sprintf("contenttype%d", i))
|
|
|
|
c.Assert(m.Gzip, qt.Equals, i != 0)
|
|
|
|
c.Assert(m.Force, qt.Equals, i != 0)
|
2019-05-31 12:55:48 -04:00
|
|
|
}
|
2019-05-03 12:30:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidOrderingPattern(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2019-05-03 12:30:46 -04:00
|
|
|
|
|
|
|
tomlConfig := `
|
|
|
|
|
|
|
|
someOtherValue = "foo"
|
|
|
|
|
|
|
|
[deployment]
|
|
|
|
order = ["["] # invalid regular expression
|
|
|
|
`
|
|
|
|
cfg, err := config.FromConfigString(tomlConfig, "toml")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2019-05-03 12:30:46 -04:00
|
|
|
|
|
|
|
_, err = decodeConfig(cfg)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.Not(qt.IsNil))
|
2018-12-20 14:22:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidMatcherPattern(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2018-12-20 14:22:03 -05:00
|
|
|
|
|
|
|
tomlConfig := `
|
|
|
|
|
|
|
|
someOtherValue = "foo"
|
|
|
|
|
|
|
|
[deployment]
|
|
|
|
[[deployment.matchers]]
|
|
|
|
Pattern = "[" # invalid regular expression
|
|
|
|
`
|
|
|
|
cfg, err := config.FromConfigString(tomlConfig, "toml")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2018-12-20 14:22:03 -05:00
|
|
|
|
|
|
|
_, err = decodeConfig(cfg)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.Not(qt.IsNil))
|
2018-12-20 14:22:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDecodeConfigDefault(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2018-12-20 14:22:03 -05:00
|
|
|
|
2021-06-09 04:58:18 -04:00
|
|
|
dcfg, err := decodeConfig(config.New())
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
c.Assert(len(dcfg.Targets), qt.Equals, 0)
|
|
|
|
c.Assert(len(dcfg.Matchers), qt.Equals, 0)
|
2018-12-20 14:22:03 -05:00
|
|
|
}
|
2021-11-30 16:39:47 -05:00
|
|
|
|
|
|
|
func TestEmptyTarget(t *testing.T) {
|
|
|
|
c := qt.New(t)
|
|
|
|
|
|
|
|
tomlConfig := `
|
|
|
|
[deployment]
|
|
|
|
[[deployment.targets]]
|
|
|
|
`
|
|
|
|
cfg, err := config.FromConfigString(tomlConfig, "toml")
|
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
|
|
|
|
_, err = decodeConfig(cfg)
|
|
|
|
c.Assert(err, qt.Not(qt.IsNil))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmptyMatcher(t *testing.T) {
|
|
|
|
c := qt.New(t)
|
|
|
|
|
|
|
|
tomlConfig := `
|
|
|
|
[deployment]
|
|
|
|
[[deployment.matchers]]
|
|
|
|
`
|
|
|
|
cfg, err := config.FromConfigString(tomlConfig, "toml")
|
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
|
|
|
|
_, err = decodeConfig(cfg)
|
|
|
|
c.Assert(err, qt.Not(qt.IsNil))
|
|
|
|
}
|