Skip to content

Commit

Permalink
Merge pull request #3596 from embik/announce-tests
Browse files Browse the repository at this point in the history
`pkg/announce`: make interface functions public and add tests
  • Loading branch information
k8s-ci-robot authored May 2, 2024
2 parents 60f5ea1 + 486dddf commit df5ab32
Show file tree
Hide file tree
Showing 4 changed files with 295 additions and 24 deletions.
21 changes: 16 additions & 5 deletions pkg/announce/announce.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ limitations under the License.
package announce

import (
"errors"
"fmt"
"os"
"path/filepath"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -76,13 +76,18 @@ func NewAnnounce(opts *Options) *Announce {
}
}

// SetImplementation sets the implementation to handle file operations.
func (a *Announce) SetImplementation(i impl) {
a.impl = i
}

func (a *Announce) CreateForBranch() error {
logrus.Infof(
"Creating %s branch announcement in %s",
a.options.branch, a.options.workDir,
)

if err := a.impl.create(
if err := a.impl.Create(
a.options.workDir,
fmt.Sprintf("Kubernetes %s branch has been created", a.options.branch),
fmt.Sprintf(branchAnnouncement, a.options.branch),
Expand All @@ -104,10 +109,13 @@ func (a *Announce) CreateForRelease() error {

// Read the changelog from the specified file if we got one
if a.options.changelogFile != "" {
changelogData, err := os.ReadFile(a.options.changelogFile)
changelogData, err := a.impl.ReadChangelogFile(a.options.changelogFile)
if err != nil {
return fmt.Errorf("reading changelog html file: %w", err)
}
if len(changelogData) == 0 {
return fmt.Errorf("verifying that changelog html file '%s' is not empty", a.options.changelogFile)
}
changelog = string(changelogData)
}

Expand All @@ -117,13 +125,16 @@ func (a *Announce) CreateForRelease() error {
}

logrus.Infof("Trying to get the Go version used to build %s...", a.options.tag)
goVersion, err := a.impl.getGoVersion(a.options.tag)
goVersion, err := a.impl.GetGoVersion(a.options.tag)
if err != nil {
return err
}
if goVersion == "" {
return errors.New("verifying Go version is not empty")
}
logrus.Infof("Found the following Go version: %s", goVersion)

if err := a.impl.create(
if err := a.impl.Create(
a.options.workDir,
fmt.Sprintf("Kubernetes %s is live!", a.options.tag),
fmt.Sprintf(releaseAnnouncement,
Expand Down
176 changes: 176 additions & 0 deletions pkg/announce/announce_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
Copyright 2024 The Kubernetes Authors.
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 announce_test

import (
"errors"
"testing"

"github.com/stretchr/testify/require"

"k8s.io/release/pkg/announce"
"k8s.io/release/pkg/announce/announcefakes"
)

const (
workdir = "/workspace"
changelogFile = "/workspace/src/release-notes.html"
branch = "release-1.30"
)

var err = errors.New("error")

var readChangelogFileStub = func(s string) ([]byte, error) {
if s == changelogFile {
return []byte("<b>changelog contents</b>"), nil
} else {
return nil, err
}
}

func TestCreateForBranch(t *testing.T) {
for _, tc := range []struct {
name string
workdir string
branch string
prepare func(*announcefakes.FakeImpl)
shouldError bool
}{
{
name: "create announcement successfully",
workdir: workdir,
branch: branch,
prepare: func(mock *announcefakes.FakeImpl) {
},
shouldError: false,
},
{
name: "fails to create announcement file",
workdir: workdir,
branch: branch,
prepare: func(mock *announcefakes.FakeImpl) {
mock.CreateReturns(err)
},
shouldError: true,
},
} {
t.Run(tc.name, func(t *testing.T) {
opts := announce.NewOptions().
WithWorkDir(tc.workdir).
WithBranch(tc.branch)

an := announce.NewAnnounce(opts)
mock := &announcefakes.FakeImpl{}
tc.prepare(mock)
an.SetImplementation(mock)

err := an.CreateForBranch()
if tc.shouldError {
require.NotNil(t, err)
} else {
require.Nil(t, err)
}
})
}
}

func TestCreateForRelease(t *testing.T) {
for _, tc := range []struct {
name string
workdir string
changelogFile string
tag string
prepare func(*announcefakes.FakeImpl)
shouldError bool
}{
{
name: "create announcement successfully",
workdir: workdir,
changelogFile: changelogFile,
tag: "1.30.0",
prepare: func(mock *announcefakes.FakeImpl) {
mock.GetGoVersionReturns("1.22.0", nil)
mock.ReadChangelogFileCalls(readChangelogFileStub)
},
shouldError: false,
},
{
name: "fails to get go version",
workdir: workdir,
changelogFile: changelogFile,
tag: "1.30.0",
prepare: func(mock *announcefakes.FakeImpl) {
mock.GetGoVersionReturns("", err)
mock.ReadChangelogFileCalls(readChangelogFileStub)
},
shouldError: true,
},
{
name: "gets empty go version",
workdir: workdir,
changelogFile: changelogFile,
tag: "1.30.0",
prepare: func(mock *announcefakes.FakeImpl) {
mock.GetGoVersionReturns("", nil)
mock.ReadChangelogFileCalls(readChangelogFileStub)
},
shouldError: true,
},
{
name: "fails to create announcement file",
workdir: workdir,
changelogFile: changelogFile,
tag: "1.30.0",
prepare: func(mock *announcefakes.FakeImpl) {
mock.GetGoVersionReturns("1.22.0", nil)
mock.CreateReturns(err)
mock.ReadChangelogFileCalls(readChangelogFileStub)
},
shouldError: true,
},
{
name: "fails to read changelog file",
workdir: workdir,
changelogFile: changelogFile,
tag: "1.30.0",
prepare: func(mock *announcefakes.FakeImpl) {
mock.GetGoVersionReturns("1.22.0", nil)
mock.ReadChangelogFileReturns(nil, err)
},
shouldError: true,
},
} {
t.Run(tc.name, func(t *testing.T) {
opts := announce.NewOptions().
WithWorkDir(tc.workdir).
WithChangelogFile(tc.changelogFile).
WithTag(tc.tag)

an := announce.NewAnnounce(opts)
mock := &announcefakes.FakeImpl{}
tc.prepare(mock)
an.SetImplementation(mock)

err := an.CreateForRelease()
if tc.shouldError {
require.NotNil(t, err)
} else {
require.Nil(t, err)
}
})
}
}
Loading

0 comments on commit df5ab32

Please sign in to comment.