Comments (3)
Before starting implementation, I want to write a proposal here to discuss the architecture of this utility.
We have a CommandContext function that returns a *Cmd object. Then, this object is used for calling the CombinedOutput function. It runs the cli command and gets the output. Here we need interfaces to implement the different behaviors, real and mock.
First interface is Exec. It has CommandContext function that returns another interface Command:
type Exec interface {
CommandContext(ctx context.Context) Command
}
The main purpose of this interface is to determine the struct to be used in the GetCombinedOutput function (Real or Mock). There will be two structs that implement the CommandContext function of Exec interface:
type RealExec struct
type MockExec struct
Second interface is Command. It has GetCombinedOutput function.
type Command interface {
GetCombinedOutput() ([]byte, error)
}
There will be two structs that will implement the GetCombinedOutput function of Command interface
type RealCommand struct
type MockCommand struct
The RealCommand implementation will call the exec.CombinedOutput function. Mock implementation will set the stdOut, stdErr, exitCode etc. fields of Cmd struct. By this way, without running the cli command, we can test the behavior of workspace functions.
from terrajet.
Thanks for writing this down @sergenyalcin ! I love how you decided to start with the minimal set of functions on the interfaces that can grow over time.
type RealExec struct
type RealCommand struct
Is there a way to avoid having these structs? It seems like if we make Command interface
compatible with *exec.Cmd
methods, i.e. naming CombinedOutput
instead of GetCombinedOutput
, we can achieve that. I didn't dive deep but that's probably harder for RealExec
since original CommandContext
returns *Cmd
instead of an interface or simple type.
Another thing to keep in mind that I think we should treat this as layer between user and the underlying implementation instead of only a test utility, i.e. there could be some other implementations in the future that will use this with different back-ends like remote SSH execution. Implication of that would be for example, instead of MockCommand
, we can call it CommandInMemory
(or something not have to do with mocking) where it saves the env vars and the bash strings in a single object in memory where you can check against.
In fact, it could grow into its own open source project that will allow people do such things, similar to afero.
from terrajet.
While working on this issue, I observed that, a utility for this aim was already implemented in k8s.io/utils
:
https://github.com/kubernetes/utils/blob/master/exec/exec.go
https://github.com/kubernetes/utils/blob/master/exec/testing/fake_exec.go
It seems that, the general architecture is similar to our design and it is suitable for us. We may consider to use this utility instead of implementing the new one. So, I am closing this issue for now, In case we encounter a problem while developing the unit tests, we can continue to work on this issue again.
from terrajet.
Related Issues (20)
- Reference network interface in VM creation
- How to deploy into existing infrastructure? HOT 2
- Customizable late initialization function for a resource
- Add support for numeric sensitive fields
- PollInterval is not considered by controllers
- main.tf's error message on failed write is incorrect
- Fix security vulnerabilities with hashicorp/go-getter upgrade to v1.6.1 HOT 1
- Fix security vulnerabilities by using Go 1.19 HOT 2
- can't get the correct terraform state file when importing the existed resources HOT 3
- Update documentation and provider-jet-template repo to cover creating a crossplane provider from a CUSTOM Terraform provider
- Allow resource configuration to provide additional state file values
- Terrajet providers take a long time on cluster with many resources HOT 2
- Issue when trying to convert ldap provider
- Need tip with converting ldap provider HOT 12
- Import cycle not allowed HOT 1
- Allow prevent_destroy for provider
- Using "Test":true in TF_REATTACH_PROVIDER cause memory leaks
- Cannot generate provider crds HOT 2
- Support configuring the json tags of a field
- Deprecating Terrajet HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from terrajet.