-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforce bundle specs version to be semver (#114)
* Enforce bundle specs version to be semver * Allow Bundle Spec's with version 1.0
- Loading branch information
Showing
4 changed files
with
141 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// | ||
// Copyright (c) 2018 Red Hat, 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. | ||
// | ||
|
||
package bundle | ||
|
||
import ( | ||
"github.com/coreos/go-semver/semver" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// These constants describe the minimum and maximum | ||
// accepted APB spec versions. They are used to filter | ||
// acceptable APBs. | ||
|
||
// MinSpecVersion constant to describe minimum supported spec version | ||
const MinSpecVersion = "1.0.0" | ||
|
||
// MaxSpecVersion constant to describe maximum supported spec version | ||
const MaxSpecVersion = "1.0.0" | ||
|
||
// These constants describe the minimum and maximum | ||
// accepted APB runtime versions. They are used to filter | ||
// acceptable APBs. | ||
|
||
// MinRuntimeVersion constant to describe minimum supported runtime version | ||
const MinRuntimeVersion = 1 | ||
|
||
// MaxRuntimeVersion constant to describe maximum supported runtime version | ||
const MaxRuntimeVersion = 2 | ||
|
||
// The minimum/maximum Bundle spec semantic versions | ||
var minSpecSemver = semver.New("1.0.0") | ||
var maxSpecSemver = semver.New("1.0.0") | ||
|
||
// ValidateVersion - Ensure the Bundle Spec Version and Bundle Runtime Version | ||
// are within bounds | ||
func (s *Spec) ValidateVersion() bool { | ||
return s.checkVersion() && s.checkRuntime() | ||
} | ||
|
||
func (s *Spec) checkVersion() bool { | ||
specSemver, err := semver.NewVersion(s.Version) | ||
if err != nil { | ||
if s.Version == "1.0" { | ||
log.Warningf("Spec version is not semver compatable") | ||
return true | ||
} | ||
log.Errorf("Failed to init semver - %v", err) | ||
return false | ||
} | ||
|
||
if specSemver.Compare(*minSpecSemver) < 0 { | ||
log.Errorf("Spec version (%v) is less than the minimum version %v", s.Version, MinSpecVersion) | ||
return false | ||
} | ||
|
||
if specSemver.Compare(*maxSpecSemver) > 0 { | ||
log.Errorf("Spec version (%v) is greater than the maximum version %v", s.Version, MaxSpecVersion) | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
func (s *Spec) checkRuntime() bool { | ||
return s.Runtime >= MinRuntimeVersion && s.Runtime <= MaxRuntimeVersion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// Copyright (c) 2018 Red Hat, 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. | ||
// | ||
|
||
package bundle | ||
|
||
import ( | ||
//"fmt" | ||
"testing" | ||
|
||
ft "github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestValidateVersion(t *testing.T) { | ||
|
||
// Test Valid Spec Version + Runtime Version | ||
var testSpec = Spec{ | ||
Version: "1.0.0", | ||
Runtime: 1, | ||
} | ||
ft.True(t, testSpec.ValidateVersion()) | ||
|
||
testSpec.Runtime = 2 | ||
ft.True(t, testSpec.ValidateVersion()) | ||
|
||
testSpec.Version = "1.0" // Deprecated Spec Version | ||
ft.True(t, testSpec.ValidateVersion()) | ||
|
||
// Test Invalid Spec Versions | ||
testSpec.Version = "0.9.0" // less than min | ||
ft.False(t, testSpec.ValidateVersion()) | ||
|
||
testSpec.Version = "1.0.1" // greater than max | ||
ft.False(t, testSpec.ValidateVersion()) | ||
|
||
testSpec.Version = "1.0.0" // back to valid version | ||
ft.True(t, testSpec.ValidateVersion()) | ||
|
||
// Test Invalid Runtime Versions | ||
testSpec.Runtime = 0 | ||
ft.False(t, testSpec.ValidateVersion()) // less than min | ||
|
||
testSpec.Runtime = 3 | ||
ft.False(t, testSpec.ValidateVersion()) // greater than max | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters