2022-08-31 22:36:32 +00:00
|
|
|
// Copyright 2022 Woodpecker 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 cron
|
|
|
|
|
|
|
|
import (
|
2022-08-31 23:19:49 +00:00
|
|
|
"context"
|
2022-08-31 22:36:32 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-08-31 23:19:49 +00:00
|
|
|
"github.com/stretchr/testify/mock"
|
2022-08-31 22:36:32 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
mocks_forge "go.woodpecker-ci.org/woodpecker/v2/server/forge/mocks"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
|
|
|
mocks_store "go.woodpecker-ci.org/woodpecker/v2/server/store/mocks"
|
2022-08-31 22:36:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateBuild(t *testing.T) {
|
2022-11-04 23:35:06 +00:00
|
|
|
forge := mocks_forge.NewForge(t)
|
2022-08-31 23:19:49 +00:00
|
|
|
store := mocks_store.NewStore(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
creator := &model.User{
|
|
|
|
ID: 1,
|
|
|
|
Login: "user1",
|
|
|
|
}
|
|
|
|
repo1 := &model.Repo{
|
|
|
|
ID: 1,
|
|
|
|
Name: "repo1",
|
|
|
|
Owner: "owner1",
|
|
|
|
FullName: "repo1/owner1",
|
|
|
|
Branch: "default",
|
|
|
|
}
|
|
|
|
|
|
|
|
// mock things
|
|
|
|
store.On("GetRepo", mock.Anything).Return(repo1, nil)
|
|
|
|
store.On("GetUser", mock.Anything).Return(creator, nil)
|
2024-02-11 09:44:50 +00:00
|
|
|
forge.On("BranchHead", mock.Anything, creator, repo1, "default").Return(&model.Commit{
|
|
|
|
ForgeURL: "https://example.com/sha1",
|
|
|
|
SHA: "sha1",
|
|
|
|
}, nil)
|
2022-08-31 23:19:49 +00:00
|
|
|
|
2022-11-04 23:35:06 +00:00
|
|
|
_, pipeline, err := CreatePipeline(ctx, store, forge, &model.Cron{
|
2022-08-31 23:19:49 +00:00
|
|
|
Name: "test",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2022-10-18 01:24:12 +00:00
|
|
|
assert.EqualValues(t, &model.Pipeline{
|
2024-02-11 09:44:50 +00:00
|
|
|
Branch: "default",
|
|
|
|
Commit: "sha1",
|
|
|
|
Event: "cron",
|
|
|
|
ForgeURL: "https://example.com/sha1",
|
|
|
|
Message: "test",
|
|
|
|
Ref: "refs/heads/default",
|
|
|
|
Sender: "test",
|
2022-10-18 01:24:12 +00:00
|
|
|
}, pipeline)
|
2022-08-31 22:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCalcNewNext(t *testing.T) {
|
|
|
|
now := time.Unix(1661962369, 0)
|
|
|
|
_, err := CalcNewNext("", now)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
schedule, err := CalcNewNext("@every 5m", now)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 1661962669, schedule.Unix())
|
|
|
|
}
|