Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set fake k8s client before calling k8s.NewClient() to test #1200

Merged
merged 1 commit into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ Alternatively, run `dtm list plugins` and it will show you all the available plu
### Development Guide

- [Development Environment Setup](https://docs.devstream.io/en/latest/development/dev/dev-env-setup)
- [Code linter](https://docs.devstream.io/en/latest/development/dev/lint.md)
- [Build the source code](https://docs.devstream.io/en/latest/development/dev/build.md)
- [Code linter](https://docs.devstream.io/en/latest/development/dev/lint)
- [Build the source code](https://docs.devstream.io/en/latest/development/dev/build)
- [Test the source code: unit test, e2e test](https://docs.devstream.io/en/latest/development/dev/test)
- [Create a plugin](https://docs.devstream.io/en/latest/development/dev/creating-a-plugin)

Expand Down
2 changes: 1 addition & 1 deletion docs/development/git-workflow/reviewing.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Below are a set of common questions that apply to all pull requests:
Reviewers are encouraged to read the following articles for help with common reviewer tasks:

- [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/)
- [Project Layout](https://docs.devstream.io/en/latest/development/project-layout/)
- [Project Layout](https://docs.devstream.io/en/latest/development/devstream/project-layout/)
- [The Art of Closing: How to closing an unfinished or rejected pull request](https://blog.jessfraz.com/post/the-art-of-closing/)
- [Kindness and Code Reviews: Improving the Way We Give Feedback](https://product.voxmedia.com/2018/8/21/17549400/kindness-and-code-reviews-improving-the-way-we-give-feedback)
- [Code Review Guidelines for Humans: Examples of good and back feedback](https://phauer.com/2018/code-review-guidelines/#code-reviews-guidelines-for-the-reviewer)
2 changes: 1 addition & 1 deletion docs/development/git-workflow/reviewing.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
我们鼓励审稿人阅读以下文章,这会对常见的审查工作有所帮助:

- [常规提交](https://www.conventionalcommits.org/en/v1.0.0/)
- [项目结构](https://docs.devstream.io/en/latest/development/project-layout/)
- [项目结构](https://docs.devstream.io/en/latest/development/devstream/project-layout.zh/)
- [关闭的艺术:如何关闭未完成或被拒绝的拉取请求](https://blog.jessfraz.com/post/the-art-of-closing/)
- [善意和代码审查:改进我们反馈的方式](https://product.voxmedia.com/2018/8/21/17549400/kindness-and-code-reviews-improving-the-way-we-give-feedback)
- [代码审查指南:良好反馈和反向反馈的示例](https://phauer.com/2018/code-review-guidelines/#code-reviews-guidelines-for-the-reviewer)
19 changes: 18 additions & 1 deletion pkg/util/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ import (

type Client struct {
clientset kubernetes.Interface
argocd *argocdclient.Clientset
// maybe it is not proper to put argocd client in the "k8s client"
argocd *argocdclient.Clientset
}

var fakeClient *Client

func NewClient() (*Client, error) {
// if UseFakeClient() is called, return the fake client.
if fakeClient != nil {
return fakeClient, nil
}

// TL;DR: Don't use viper.GetString("xxx") in the `util/xxx` package.
// Don't use `kubeconfig := viper.GetString("kubeconfig")` here,
// it will fail without calling `viper.BindEnv("github_token")` first.
Expand Down Expand Up @@ -59,3 +67,12 @@ func NewClient() (*Client, error) {
argocd: argocdClientset,
}, nil
}

// UseFakeClient is used for testing,
// if this function is called, NewClient() will return the fake client.
func UseFakeClient(k8sClient kubernetes.Interface, argoClient *argocdclient.Clientset) {
fakeClient = &Client{
clientset: k8sClient,
argocd: argoClient,
}
}
2 changes: 1 addition & 1 deletion pkg/util/scm/github/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (c *Client) VerifyWorkflows(workflows []*Workflow) (map[string]error, error
filesInRemoteDir = append(filesInRemoteDir, f.GetName())
}

lostFiles := mapset.NewSet[string](wsFiles...).Difference(mapset.NewSet[string](filesInRemoteDir...)).ToSlice()
lostFiles := mapset.NewSet(wsFiles...).Difference(mapset.NewSet(filesInRemoteDir...)).ToSlice()
// all files exist
if len(lostFiles) == 0 {
log.Info("All files exist.")
Expand Down